tipc: use pseudo message to wake up sockets after link congestion
[deliverable/linux.git] / net / tipc / node.c
1 /*
2 * net/tipc/node.c: TIPC node management routines
3 *
4 * Copyright (c) 2000-2006, 2012-2014, Ericsson AB
5 * Copyright (c) 2005-2006, 2010-2014, Wind River Systems
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
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.
19 *
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.
23 *
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.
35 */
36
37 #include "core.h"
38 #include "config.h"
39 #include "node.h"
40 #include "name_distr.h"
41 #include "socket.h"
42
43 #define NODE_HTABLE_SIZE 512
44
45 static void node_lost_contact(struct tipc_node *n_ptr);
46 static void node_established_contact(struct tipc_node *n_ptr);
47
48 static struct hlist_head node_htable[NODE_HTABLE_SIZE];
49 LIST_HEAD(tipc_node_list);
50 static u32 tipc_num_nodes;
51 static u32 tipc_num_links;
52 static DEFINE_SPINLOCK(node_list_lock);
53
54 /*
55 * A trivial power-of-two bitmask technique is used for speed, since this
56 * operation is done for every incoming TIPC packet. The number of hash table
57 * entries has been chosen so that no hash chain exceeds 8 nodes and will
58 * usually be much smaller (typically only a single node).
59 */
60 static unsigned int tipc_hashfn(u32 addr)
61 {
62 return addr & (NODE_HTABLE_SIZE - 1);
63 }
64
65 /*
66 * tipc_node_find - locate specified node object, if it exists
67 */
68 struct tipc_node *tipc_node_find(u32 addr)
69 {
70 struct tipc_node *node;
71
72 if (unlikely(!in_own_cluster_exact(addr)))
73 return NULL;
74
75 rcu_read_lock();
76 hlist_for_each_entry_rcu(node, &node_htable[tipc_hashfn(addr)], hash) {
77 if (node->addr == addr) {
78 rcu_read_unlock();
79 return node;
80 }
81 }
82 rcu_read_unlock();
83 return NULL;
84 }
85
86 struct tipc_node *tipc_node_create(u32 addr)
87 {
88 struct tipc_node *n_ptr, *temp_node;
89
90 spin_lock_bh(&node_list_lock);
91
92 n_ptr = kzalloc(sizeof(*n_ptr), GFP_ATOMIC);
93 if (!n_ptr) {
94 spin_unlock_bh(&node_list_lock);
95 pr_warn("Node creation failed, no memory\n");
96 return NULL;
97 }
98
99 n_ptr->addr = addr;
100 spin_lock_init(&n_ptr->lock);
101 INIT_HLIST_NODE(&n_ptr->hash);
102 INIT_LIST_HEAD(&n_ptr->list);
103 INIT_LIST_HEAD(&n_ptr->nsub);
104 __skb_queue_head_init(&n_ptr->waiting_sks);
105
106 hlist_add_head_rcu(&n_ptr->hash, &node_htable[tipc_hashfn(addr)]);
107
108 list_for_each_entry_rcu(temp_node, &tipc_node_list, list) {
109 if (n_ptr->addr < temp_node->addr)
110 break;
111 }
112 list_add_tail_rcu(&n_ptr->list, &temp_node->list);
113 n_ptr->action_flags = TIPC_WAIT_PEER_LINKS_DOWN;
114 n_ptr->signature = INVALID_NODE_SIG;
115
116 tipc_num_nodes++;
117
118 spin_unlock_bh(&node_list_lock);
119 return n_ptr;
120 }
121
122 static void tipc_node_delete(struct tipc_node *n_ptr)
123 {
124 list_del_rcu(&n_ptr->list);
125 hlist_del_rcu(&n_ptr->hash);
126 kfree_rcu(n_ptr, rcu);
127
128 tipc_num_nodes--;
129 }
130
131 void tipc_node_stop(void)
132 {
133 struct tipc_node *node, *t_node;
134
135 spin_lock_bh(&node_list_lock);
136 list_for_each_entry_safe(node, t_node, &tipc_node_list, list)
137 tipc_node_delete(node);
138 spin_unlock_bh(&node_list_lock);
139 }
140
141 /**
142 * tipc_node_link_up - handle addition of link
143 *
144 * Link becomes active (alone or shared) or standby, depending on its priority.
145 */
146 void tipc_node_link_up(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
147 {
148 struct tipc_link **active = &n_ptr->active_links[0];
149 u32 addr = n_ptr->addr;
150
151 n_ptr->working_links++;
152 tipc_nametbl_publish(TIPC_LINK_STATE, addr, addr, TIPC_NODE_SCOPE,
153 l_ptr->bearer_id, addr);
154 pr_info("Established link <%s> on network plane %c\n",
155 l_ptr->name, l_ptr->net_plane);
156
157 if (!active[0]) {
158 active[0] = active[1] = l_ptr;
159 node_established_contact(n_ptr);
160 goto exit;
161 }
162 if (l_ptr->priority < active[0]->priority) {
163 pr_info("New link <%s> becomes standby\n", l_ptr->name);
164 goto exit;
165 }
166 tipc_link_dup_queue_xmit(active[0], l_ptr);
167 if (l_ptr->priority == active[0]->priority) {
168 active[0] = l_ptr;
169 goto exit;
170 }
171 pr_info("Old link <%s> becomes standby\n", active[0]->name);
172 if (active[1] != active[0])
173 pr_info("Old link <%s> becomes standby\n", active[1]->name);
174 active[0] = active[1] = l_ptr;
175 exit:
176 /* Leave room for changeover header when returning 'mtu' to users: */
177 n_ptr->act_mtus[0] = active[0]->max_pkt - INT_H_SIZE;
178 n_ptr->act_mtus[1] = active[1]->max_pkt - INT_H_SIZE;
179 }
180
181 /**
182 * node_select_active_links - select active link
183 */
184 static void node_select_active_links(struct tipc_node *n_ptr)
185 {
186 struct tipc_link **active = &n_ptr->active_links[0];
187 u32 i;
188 u32 highest_prio = 0;
189
190 active[0] = active[1] = NULL;
191
192 for (i = 0; i < MAX_BEARERS; i++) {
193 struct tipc_link *l_ptr = n_ptr->links[i];
194
195 if (!l_ptr || !tipc_link_is_up(l_ptr) ||
196 (l_ptr->priority < highest_prio))
197 continue;
198
199 if (l_ptr->priority > highest_prio) {
200 highest_prio = l_ptr->priority;
201 active[0] = active[1] = l_ptr;
202 } else {
203 active[1] = l_ptr;
204 }
205 }
206 }
207
208 /**
209 * tipc_node_link_down - handle loss of link
210 */
211 void tipc_node_link_down(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
212 {
213 struct tipc_link **active;
214 u32 addr = n_ptr->addr;
215
216 n_ptr->working_links--;
217 tipc_nametbl_withdraw(TIPC_LINK_STATE, addr, l_ptr->bearer_id, addr);
218
219 if (!tipc_link_is_active(l_ptr)) {
220 pr_info("Lost standby link <%s> on network plane %c\n",
221 l_ptr->name, l_ptr->net_plane);
222 return;
223 }
224 pr_info("Lost link <%s> on network plane %c\n",
225 l_ptr->name, l_ptr->net_plane);
226
227 active = &n_ptr->active_links[0];
228 if (active[0] == l_ptr)
229 active[0] = active[1];
230 if (active[1] == l_ptr)
231 active[1] = active[0];
232 if (active[0] == l_ptr)
233 node_select_active_links(n_ptr);
234 if (tipc_node_is_up(n_ptr))
235 tipc_link_failover_send_queue(l_ptr);
236 else
237 node_lost_contact(n_ptr);
238
239 /* Leave room for changeover header when returning 'mtu' to users: */
240 if (active[0]) {
241 n_ptr->act_mtus[0] = active[0]->max_pkt - INT_H_SIZE;
242 n_ptr->act_mtus[1] = active[1]->max_pkt - INT_H_SIZE;
243 return;
244 }
245
246 /* Loopback link went down? No fragmentation needed from now on. */
247 if (n_ptr->addr == tipc_own_addr) {
248 n_ptr->act_mtus[0] = MAX_MSG_SIZE;
249 n_ptr->act_mtus[1] = MAX_MSG_SIZE;
250 }
251 }
252
253 int tipc_node_active_links(struct tipc_node *n_ptr)
254 {
255 return n_ptr->active_links[0] != NULL;
256 }
257
258 int tipc_node_is_up(struct tipc_node *n_ptr)
259 {
260 return tipc_node_active_links(n_ptr);
261 }
262
263 void tipc_node_attach_link(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
264 {
265 n_ptr->links[l_ptr->bearer_id] = l_ptr;
266 spin_lock_bh(&node_list_lock);
267 tipc_num_links++;
268 spin_unlock_bh(&node_list_lock);
269 n_ptr->link_cnt++;
270 }
271
272 void tipc_node_detach_link(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
273 {
274 int i;
275
276 for (i = 0; i < MAX_BEARERS; i++) {
277 if (l_ptr != n_ptr->links[i])
278 continue;
279 n_ptr->links[i] = NULL;
280 spin_lock_bh(&node_list_lock);
281 tipc_num_links--;
282 spin_unlock_bh(&node_list_lock);
283 n_ptr->link_cnt--;
284 }
285 }
286
287 static void node_established_contact(struct tipc_node *n_ptr)
288 {
289 n_ptr->action_flags |= TIPC_NOTIFY_NODE_UP;
290 n_ptr->bclink.oos_state = 0;
291 n_ptr->bclink.acked = tipc_bclink_get_last_sent();
292 tipc_bclink_add_node(n_ptr->addr);
293 }
294
295 static void node_lost_contact(struct tipc_node *n_ptr)
296 {
297 char addr_string[16];
298 u32 i;
299
300 pr_info("Lost contact with %s\n",
301 tipc_addr_string_fill(addr_string, n_ptr->addr));
302
303 /* Flush broadcast link info associated with lost node */
304 if (n_ptr->bclink.recv_permitted) {
305 kfree_skb_list(n_ptr->bclink.deferred_head);
306 n_ptr->bclink.deferred_size = 0;
307
308 if (n_ptr->bclink.reasm_buf) {
309 kfree_skb(n_ptr->bclink.reasm_buf);
310 n_ptr->bclink.reasm_buf = NULL;
311 }
312
313 tipc_bclink_remove_node(n_ptr->addr);
314 tipc_bclink_acknowledge(n_ptr, INVALID_LINK_SEQ);
315
316 n_ptr->bclink.recv_permitted = false;
317 }
318
319 /* Abort link changeover */
320 for (i = 0; i < MAX_BEARERS; i++) {
321 struct tipc_link *l_ptr = n_ptr->links[i];
322 if (!l_ptr)
323 continue;
324 l_ptr->reset_checkpoint = l_ptr->next_in_no;
325 l_ptr->exp_msg_count = 0;
326 tipc_link_reset_fragments(l_ptr);
327 }
328
329 n_ptr->action_flags &= ~TIPC_WAIT_OWN_LINKS_DOWN;
330
331 /* Notify subscribers and prevent re-contact with node until
332 * cleanup is done.
333 */
334 n_ptr->action_flags |= TIPC_WAIT_PEER_LINKS_DOWN |
335 TIPC_NOTIFY_NODE_DOWN;
336 }
337
338 struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space)
339 {
340 u32 domain;
341 struct sk_buff *buf;
342 struct tipc_node *n_ptr;
343 struct tipc_node_info node_info;
344 u32 payload_size;
345
346 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
347 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
348
349 domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
350 if (!tipc_addr_domain_valid(domain))
351 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
352 " (network address)");
353
354 spin_lock_bh(&node_list_lock);
355 if (!tipc_num_nodes) {
356 spin_unlock_bh(&node_list_lock);
357 return tipc_cfg_reply_none();
358 }
359
360 /* For now, get space for all other nodes */
361 payload_size = TLV_SPACE(sizeof(node_info)) * tipc_num_nodes;
362 if (payload_size > 32768u) {
363 spin_unlock_bh(&node_list_lock);
364 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
365 " (too many nodes)");
366 }
367 spin_unlock_bh(&node_list_lock);
368
369 buf = tipc_cfg_reply_alloc(payload_size);
370 if (!buf)
371 return NULL;
372
373 /* Add TLVs for all nodes in scope */
374 rcu_read_lock();
375 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
376 if (!tipc_in_scope(domain, n_ptr->addr))
377 continue;
378 node_info.addr = htonl(n_ptr->addr);
379 node_info.up = htonl(tipc_node_is_up(n_ptr));
380 tipc_cfg_append_tlv(buf, TIPC_TLV_NODE_INFO,
381 &node_info, sizeof(node_info));
382 }
383 rcu_read_unlock();
384 return buf;
385 }
386
387 struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space)
388 {
389 u32 domain;
390 struct sk_buff *buf;
391 struct tipc_node *n_ptr;
392 struct tipc_link_info link_info;
393 u32 payload_size;
394
395 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NET_ADDR))
396 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
397
398 domain = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
399 if (!tipc_addr_domain_valid(domain))
400 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
401 " (network address)");
402
403 if (!tipc_own_addr)
404 return tipc_cfg_reply_none();
405
406 spin_lock_bh(&node_list_lock);
407 /* Get space for all unicast links + broadcast link */
408 payload_size = TLV_SPACE((sizeof(link_info)) * (tipc_num_links + 1));
409 if (payload_size > 32768u) {
410 spin_unlock_bh(&node_list_lock);
411 return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
412 " (too many links)");
413 }
414 spin_unlock_bh(&node_list_lock);
415
416 buf = tipc_cfg_reply_alloc(payload_size);
417 if (!buf)
418 return NULL;
419
420 /* Add TLV for broadcast link */
421 link_info.dest = htonl(tipc_cluster_mask(tipc_own_addr));
422 link_info.up = htonl(1);
423 strlcpy(link_info.str, tipc_bclink_name, TIPC_MAX_LINK_NAME);
424 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO, &link_info, sizeof(link_info));
425
426 /* Add TLVs for any other links in scope */
427 rcu_read_lock();
428 list_for_each_entry_rcu(n_ptr, &tipc_node_list, list) {
429 u32 i;
430
431 if (!tipc_in_scope(domain, n_ptr->addr))
432 continue;
433 tipc_node_lock(n_ptr);
434 for (i = 0; i < MAX_BEARERS; i++) {
435 if (!n_ptr->links[i])
436 continue;
437 link_info.dest = htonl(n_ptr->addr);
438 link_info.up = htonl(tipc_link_is_up(n_ptr->links[i]));
439 strcpy(link_info.str, n_ptr->links[i]->name);
440 tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO,
441 &link_info, sizeof(link_info));
442 }
443 tipc_node_unlock(n_ptr);
444 }
445 rcu_read_unlock();
446 return buf;
447 }
448
449 /**
450 * tipc_node_get_linkname - get the name of a link
451 *
452 * @bearer_id: id of the bearer
453 * @node: peer node address
454 * @linkname: link name output buffer
455 *
456 * Returns 0 on success
457 */
458 int tipc_node_get_linkname(u32 bearer_id, u32 addr, char *linkname, size_t len)
459 {
460 struct tipc_link *link;
461 struct tipc_node *node = tipc_node_find(addr);
462
463 if ((bearer_id >= MAX_BEARERS) || !node)
464 return -EINVAL;
465 tipc_node_lock(node);
466 link = node->links[bearer_id];
467 if (link) {
468 strncpy(linkname, link->name, len);
469 tipc_node_unlock(node);
470 return 0;
471 }
472 tipc_node_unlock(node);
473 return -EINVAL;
474 }
475
476 void tipc_node_unlock(struct tipc_node *node)
477 {
478 LIST_HEAD(nsub_list);
479 struct sk_buff_head waiting_sks;
480 u32 addr = 0;
481
482 if (likely(!node->action_flags)) {
483 spin_unlock_bh(&node->lock);
484 return;
485 }
486
487 __skb_queue_head_init(&waiting_sks);
488 if (node->action_flags & TIPC_WAKEUP_USERS) {
489 skb_queue_splice_init(&node->waiting_sks, &waiting_sks);
490 node->action_flags &= ~TIPC_WAKEUP_USERS;
491 }
492 if (node->action_flags & TIPC_NOTIFY_NODE_DOWN) {
493 list_replace_init(&node->nsub, &nsub_list);
494 node->action_flags &= ~TIPC_NOTIFY_NODE_DOWN;
495 }
496 if (node->action_flags & TIPC_NOTIFY_NODE_UP) {
497 node->action_flags &= ~TIPC_NOTIFY_NODE_UP;
498 addr = node->addr;
499 }
500 spin_unlock_bh(&node->lock);
501
502 while (!skb_queue_empty(&waiting_sks))
503 tipc_sk_rcv(__skb_dequeue(&waiting_sks));
504
505 if (!list_empty(&nsub_list))
506 tipc_nodesub_notify(&nsub_list);
507
508 if (addr)
509 tipc_named_node_up(addr);
510 }
This page took 0.042629 seconds and 5 git commands to generate.