arm64: Use DMA_ERROR_CODE to denote failed allocation
[linux-2.6/btrfs-unstable.git] / net / tipc / port.c
blob7e096a5e770156631e701c91fc8c5ac4a34c5ce9
1 /*
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
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"
41 #include "socket.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);
56 /**
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)
64 u32 peernode;
65 u32 orignode;
67 if (msg_origport(msg) != tipc_port_peerport(p_ptr))
68 return 0;
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));
77 /* tipc_port_init - intiate TIPC port and lock it
79 * Returns obtained reference if initialization is successful, zero otherwise
81 u32 tipc_port_init(struct tipc_port *p_ptr,
82 const unsigned int importance)
84 struct tipc_msg *msg;
85 u32 ref;
87 ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
88 if (!ref) {
89 pr_warn("Port registration failed, ref. table exhausted\n");
90 return 0;
93 p_ptr->max_pkt = MAX_PKT_DEFAULT;
94 p_ptr->ref = ref;
95 INIT_LIST_HEAD(&p_ptr->wait_list);
96 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
97 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
98 INIT_LIST_HEAD(&p_ptr->publications);
99 INIT_LIST_HEAD(&p_ptr->port_list);
102 * Must hold port list lock while initializing message header template
103 * to ensure a change to node's own network address doesn't result
104 * in template containing out-dated network address information
106 spin_lock_bh(&tipc_port_list_lock);
107 msg = &p_ptr->phdr;
108 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
109 msg_set_origport(msg, ref);
110 list_add_tail(&p_ptr->port_list, &ports);
111 spin_unlock_bh(&tipc_port_list_lock);
112 return ref;
115 void tipc_port_destroy(struct tipc_port *p_ptr)
117 struct sk_buff *buf = NULL;
118 struct tipc_msg *msg = NULL;
119 u32 peer;
121 tipc_withdraw(p_ptr, 0, NULL);
123 spin_lock_bh(p_ptr->lock);
124 tipc_ref_discard(p_ptr->ref);
125 spin_unlock_bh(p_ptr->lock);
127 k_cancel_timer(&p_ptr->timer);
128 if (p_ptr->connected) {
129 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
130 tipc_nodesub_unsubscribe(&p_ptr->subscription);
131 msg = buf_msg(buf);
132 peer = msg_destnode(msg);
133 tipc_link_xmit(buf, peer, msg_link_selector(msg));
135 spin_lock_bh(&tipc_port_list_lock);
136 list_del(&p_ptr->port_list);
137 list_del(&p_ptr->wait_list);
138 spin_unlock_bh(&tipc_port_list_lock);
139 k_term_timer(&p_ptr->timer);
143 * port_build_proto_msg(): create connection protocol message for port
145 * On entry the port must be locked and connected.
147 static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
148 u32 type, u32 ack)
150 struct sk_buff *buf;
151 struct tipc_msg *msg;
153 buf = tipc_buf_acquire(INT_H_SIZE);
154 if (buf) {
155 msg = buf_msg(buf);
156 tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
157 tipc_port_peernode(p_ptr));
158 msg_set_destport(msg, tipc_port_peerport(p_ptr));
159 msg_set_origport(msg, p_ptr->ref);
160 msg_set_msgcnt(msg, ack);
161 buf->next = NULL;
163 return buf;
166 static void port_timeout(unsigned long ref)
168 struct tipc_port *p_ptr = tipc_port_lock(ref);
169 struct sk_buff *buf = NULL;
170 struct tipc_msg *msg = NULL;
172 if (!p_ptr)
173 return;
175 if (!p_ptr->connected) {
176 tipc_port_unlock(p_ptr);
177 return;
180 /* Last probe answered ? */
181 if (p_ptr->probing_state == TIPC_CONN_PROBING) {
182 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
183 } else {
184 buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
185 p_ptr->probing_state = TIPC_CONN_PROBING;
186 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
188 tipc_port_unlock(p_ptr);
189 msg = buf_msg(buf);
190 tipc_link_xmit(buf, msg_destnode(msg), msg_link_selector(msg));
194 static void port_handle_node_down(unsigned long ref)
196 struct tipc_port *p_ptr = tipc_port_lock(ref);
197 struct sk_buff *buf = NULL;
198 struct tipc_msg *msg = NULL;
200 if (!p_ptr)
201 return;
202 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
203 tipc_port_unlock(p_ptr);
204 msg = buf_msg(buf);
205 tipc_link_xmit(buf, msg_destnode(msg), msg_link_selector(msg));
209 static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
211 struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
213 if (buf) {
214 struct tipc_msg *msg = buf_msg(buf);
215 msg_swap_words(msg, 4, 5);
216 msg_swap_words(msg, 6, 7);
217 buf->next = NULL;
219 return buf;
223 static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
225 struct sk_buff *buf;
226 struct tipc_msg *msg;
227 u32 imp;
229 if (!p_ptr->connected)
230 return NULL;
232 buf = tipc_buf_acquire(BASIC_H_SIZE);
233 if (buf) {
234 msg = buf_msg(buf);
235 memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
236 msg_set_hdr_sz(msg, BASIC_H_SIZE);
237 msg_set_size(msg, BASIC_H_SIZE);
238 imp = msg_importance(msg);
239 if (imp < TIPC_CRITICAL_IMPORTANCE)
240 msg_set_importance(msg, ++imp);
241 msg_set_errcode(msg, err);
242 buf->next = NULL;
244 return buf;
247 static int port_print(struct tipc_port *p_ptr, char *buf, int len, int full_id)
249 struct publication *publ;
250 int ret;
252 if (full_id)
253 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
254 tipc_zone(tipc_own_addr),
255 tipc_cluster(tipc_own_addr),
256 tipc_node(tipc_own_addr), p_ptr->ref);
257 else
258 ret = tipc_snprintf(buf, len, "%-10u:", p_ptr->ref);
260 if (p_ptr->connected) {
261 u32 dport = tipc_port_peerport(p_ptr);
262 u32 destnode = tipc_port_peernode(p_ptr);
264 ret += tipc_snprintf(buf + ret, len - ret,
265 " connected to <%u.%u.%u:%u>",
266 tipc_zone(destnode),
267 tipc_cluster(destnode),
268 tipc_node(destnode), dport);
269 if (p_ptr->conn_type != 0)
270 ret += tipc_snprintf(buf + ret, len - ret,
271 " via {%u,%u}", p_ptr->conn_type,
272 p_ptr->conn_instance);
273 } else if (p_ptr->published) {
274 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
275 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
276 if (publ->lower == publ->upper)
277 ret += tipc_snprintf(buf + ret, len - ret,
278 " {%u,%u}", publ->type,
279 publ->lower);
280 else
281 ret += tipc_snprintf(buf + ret, len - ret,
282 " {%u,%u,%u}", publ->type,
283 publ->lower, publ->upper);
286 ret += tipc_snprintf(buf + ret, len - ret, "\n");
287 return ret;
290 struct sk_buff *tipc_port_get_ports(void)
292 struct sk_buff *buf;
293 struct tlv_desc *rep_tlv;
294 char *pb;
295 int pb_len;
296 struct tipc_port *p_ptr;
297 int str_len = 0;
299 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
300 if (!buf)
301 return NULL;
302 rep_tlv = (struct tlv_desc *)buf->data;
303 pb = TLV_DATA(rep_tlv);
304 pb_len = ULTRA_STRING_MAX_LEN;
306 spin_lock_bh(&tipc_port_list_lock);
307 list_for_each_entry(p_ptr, &ports, port_list) {
308 spin_lock_bh(p_ptr->lock);
309 str_len += port_print(p_ptr, pb, pb_len, 0);
310 spin_unlock_bh(p_ptr->lock);
312 spin_unlock_bh(&tipc_port_list_lock);
313 str_len += 1; /* for "\0" */
314 skb_put(buf, TLV_SPACE(str_len));
315 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
317 return buf;
320 void tipc_port_reinit(void)
322 struct tipc_port *p_ptr;
323 struct tipc_msg *msg;
325 spin_lock_bh(&tipc_port_list_lock);
326 list_for_each_entry(p_ptr, &ports, port_list) {
327 msg = &p_ptr->phdr;
328 msg_set_prevnode(msg, tipc_own_addr);
329 msg_set_orignode(msg, tipc_own_addr);
331 spin_unlock_bh(&tipc_port_list_lock);
334 void tipc_acknowledge(u32 ref, u32 ack)
336 struct tipc_port *p_ptr;
337 struct sk_buff *buf = NULL;
338 struct tipc_msg *msg;
340 p_ptr = tipc_port_lock(ref);
341 if (!p_ptr)
342 return;
343 if (p_ptr->connected)
344 buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
346 tipc_port_unlock(p_ptr);
347 if (!buf)
348 return;
349 msg = buf_msg(buf);
350 tipc_link_xmit(buf, msg_destnode(msg), msg_link_selector(msg));
353 int tipc_publish(struct tipc_port *p_ptr, unsigned int scope,
354 struct tipc_name_seq const *seq)
356 struct publication *publ;
357 u32 key;
359 if (p_ptr->connected)
360 return -EINVAL;
361 key = p_ptr->ref + p_ptr->pub_count + 1;
362 if (key == p_ptr->ref)
363 return -EADDRINUSE;
365 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
366 scope, p_ptr->ref, key);
367 if (publ) {
368 list_add(&publ->pport_list, &p_ptr->publications);
369 p_ptr->pub_count++;
370 p_ptr->published = 1;
371 return 0;
373 return -EINVAL;
376 int tipc_withdraw(struct tipc_port *p_ptr, unsigned int scope,
377 struct tipc_name_seq const *seq)
379 struct publication *publ;
380 struct publication *tpubl;
381 int res = -EINVAL;
383 if (!seq) {
384 list_for_each_entry_safe(publ, tpubl,
385 &p_ptr->publications, pport_list) {
386 tipc_nametbl_withdraw(publ->type, publ->lower,
387 publ->ref, publ->key);
389 res = 0;
390 } else {
391 list_for_each_entry_safe(publ, tpubl,
392 &p_ptr->publications, pport_list) {
393 if (publ->scope != scope)
394 continue;
395 if (publ->type != seq->type)
396 continue;
397 if (publ->lower != seq->lower)
398 continue;
399 if (publ->upper != seq->upper)
400 break;
401 tipc_nametbl_withdraw(publ->type, publ->lower,
402 publ->ref, publ->key);
403 res = 0;
404 break;
407 if (list_empty(&p_ptr->publications))
408 p_ptr->published = 0;
409 return res;
412 int tipc_port_connect(u32 ref, struct tipc_portid const *peer)
414 struct tipc_port *p_ptr;
415 int res;
417 p_ptr = tipc_port_lock(ref);
418 if (!p_ptr)
419 return -EINVAL;
420 res = __tipc_port_connect(ref, p_ptr, peer);
421 tipc_port_unlock(p_ptr);
422 return res;
426 * __tipc_port_connect - connect to a remote peer
428 * Port must be locked.
430 int __tipc_port_connect(u32 ref, struct tipc_port *p_ptr,
431 struct tipc_portid const *peer)
433 struct tipc_msg *msg;
434 int res = -EINVAL;
436 if (p_ptr->published || p_ptr->connected)
437 goto exit;
438 if (!peer->ref)
439 goto exit;
441 msg = &p_ptr->phdr;
442 msg_set_destnode(msg, peer->node);
443 msg_set_destport(msg, peer->ref);
444 msg_set_type(msg, TIPC_CONN_MSG);
445 msg_set_lookup_scope(msg, 0);
446 msg_set_hdr_sz(msg, SHORT_H_SIZE);
448 p_ptr->probing_interval = PROBING_INTERVAL;
449 p_ptr->probing_state = TIPC_CONN_OK;
450 p_ptr->connected = 1;
451 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
453 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
454 (void *)(unsigned long)ref,
455 (net_ev_handler)port_handle_node_down);
456 res = 0;
457 exit:
458 p_ptr->max_pkt = tipc_node_get_mtu(peer->node, ref);
459 return res;
463 * __tipc_disconnect - disconnect port from peer
465 * Port must be locked.
467 int __tipc_port_disconnect(struct tipc_port *tp_ptr)
469 if (tp_ptr->connected) {
470 tp_ptr->connected = 0;
471 /* let timer expire on it's own to avoid deadlock! */
472 tipc_nodesub_unsubscribe(&tp_ptr->subscription);
473 return 0;
476 return -ENOTCONN;
480 * tipc_port_disconnect(): Disconnect port form peer.
481 * This is a node local operation.
483 int tipc_port_disconnect(u32 ref)
485 struct tipc_port *p_ptr;
486 int res;
488 p_ptr = tipc_port_lock(ref);
489 if (!p_ptr)
490 return -EINVAL;
491 res = __tipc_port_disconnect(p_ptr);
492 tipc_port_unlock(p_ptr);
493 return res;
497 * tipc_port_shutdown(): Send a SHUTDOWN msg to peer and disconnect
499 int tipc_port_shutdown(u32 ref)
501 struct tipc_msg *msg;
502 struct tipc_port *p_ptr;
503 struct sk_buff *buf = NULL;
505 p_ptr = tipc_port_lock(ref);
506 if (!p_ptr)
507 return -EINVAL;
509 buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
510 tipc_port_unlock(p_ptr);
511 msg = buf_msg(buf);
512 tipc_link_xmit(buf, msg_destnode(msg), msg_link_selector(msg));
513 return tipc_port_disconnect(ref);