tipc: Register new media using pre-compiled structure
[linux-2.6-block.git] / net / tipc / bearer.c
CommitLineData
b97bf3fd
PL
1/*
2 * net/tipc/bearer.c: TIPC bearer code
c4307285 3 *
593a5f22 4 * Copyright (c) 1996-2006, Ericsson AB
2d627b92 5 * Copyright (c) 2004-2006, 2010-2011, Wind River Systems
b97bf3fd
PL
6 * All rights reserved.
7 *
9ea1fd3c 8 * Redistribution and use in source and binary forms, with or without
b97bf3fd
PL
9 * modification, are permitted provided that the following conditions are met:
10 *
9ea1fd3c
PL
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.
b97bf3fd 19 *
9ea1fd3c
PL
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
b97bf3fd
PL
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
38#include "config.h"
b97bf3fd 39#include "bearer.h"
b97bf3fd 40#include "discover.h"
b97bf3fd
PL
41
42#define MAX_ADDR_STR 32
43
d0021b25 44static struct media media_list[MAX_MEDIA];
e3ec9c7d 45static u32 media_count;
b97bf3fd 46
2d627b92 47struct tipc_bearer tipc_bearers[MAX_BEARERS];
b97bf3fd 48
3a777ff8
AS
49static void bearer_disable(struct tipc_bearer *b_ptr);
50
b97bf3fd
PL
51/**
52 * media_name_valid - validate media name
c4307285 53 *
b97bf3fd
PL
54 * Returns 1 if media name is valid, otherwise 0.
55 */
56
57static int media_name_valid(const char *name)
58{
59 u32 len;
60
61 len = strlen(name);
62 if ((len + 1) > TIPC_MAX_MEDIA_NAME)
63 return 0;
a02cec21 64 return strspn(name, tipc_alphabet) == len;
b97bf3fd
PL
65}
66
67/**
68 * media_find - locates specified media object by name
69 */
70
71static struct media *media_find(const char *name)
72{
73 struct media *m_ptr;
74 u32 i;
75
76 for (i = 0, m_ptr = media_list; i < media_count; i++, m_ptr++) {
77 if (!strcmp(m_ptr->name, name))
78 return m_ptr;
79 }
1fc54d8f 80 return NULL;
b97bf3fd
PL
81}
82
83/**
84 * tipc_register_media - register a media type
c4307285 85 *
b97bf3fd
PL
86 * Bearers for this media type must be activated separately at a later stage.
87 */
88
706767da 89int tipc_register_media(struct media *m_ptr)
b97bf3fd 90{
b97bf3fd
PL
91 u32 media_id;
92 u32 i;
93 int res = -EINVAL;
94
4323add6 95 write_lock_bh(&tipc_net_lock);
b97bf3fd 96
d0021b25 97 if (tipc_mode != TIPC_NET_MODE) {
706767da
AS
98 warn("Media <%s> rejected, not in networked mode yet\n",
99 m_ptr->name);
d0021b25
NH
100 goto exit;
101 }
706767da
AS
102 if (!media_name_valid(m_ptr->name)) {
103 warn("Media <%s> rejected, illegal name\n", m_ptr->name);
b97bf3fd
PL
104 goto exit;
105 }
706767da
AS
106 if (m_ptr->bcast_addr.type != htonl(m_ptr->type_id)) {
107 warn("Media <%s> rejected, illegal broadcast address\n",
108 m_ptr->name);
b97bf3fd
PL
109 goto exit;
110 }
706767da
AS
111 if ((m_ptr->priority < TIPC_MIN_LINK_PRI) ||
112 (m_ptr->priority > TIPC_MAX_LINK_PRI)) {
113 warn("Media <%s> rejected, illegal priority (%u)\n",
114 m_ptr->name, m_ptr->priority);
b97bf3fd
PL
115 goto exit;
116 }
706767da
AS
117 if ((m_ptr->tolerance < TIPC_MIN_LINK_TOL) ||
118 (m_ptr->tolerance > TIPC_MAX_LINK_TOL)) {
119 warn("Media <%s> rejected, illegal tolerance (%u)\n",
120 m_ptr->name, m_ptr->tolerance);
b97bf3fd
PL
121 goto exit;
122 }
123
124 media_id = media_count++;
125 if (media_id >= MAX_MEDIA) {
706767da
AS
126 warn("Media <%s> rejected, media limit reached (%u)\n",
127 m_ptr->name, MAX_MEDIA);
b97bf3fd
PL
128 media_count--;
129 goto exit;
130 }
131 for (i = 0; i < media_id; i++) {
706767da
AS
132 if (media_list[i].type_id == m_ptr->type_id) {
133 warn("Media <%s> rejected, duplicate type (%u)\n",
134 m_ptr->name, m_ptr->type_id);
b97bf3fd
PL
135 media_count--;
136 goto exit;
137 }
706767da
AS
138 if (!strcmp(m_ptr->name, media_list[i].name)) {
139 warn("Media <%s> rejected, duplicate name\n",
140 m_ptr->name);
b97bf3fd
PL
141 media_count--;
142 goto exit;
143 }
144 }
145
706767da 146 media_list[media_id] = *m_ptr;
b97bf3fd
PL
147 res = 0;
148exit:
4323add6 149 write_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
150 return res;
151}
152
153/**
4323add6 154 * tipc_media_addr_printf - record media address in print buffer
b97bf3fd
PL
155 */
156
4323add6 157void tipc_media_addr_printf(struct print_buf *pb, struct tipc_media_addr *a)
b97bf3fd
PL
158{
159 struct media *m_ptr;
160 u32 media_type;
161 u32 i;
162
163 media_type = ntohl(a->type);
164 for (i = 0, m_ptr = media_list; i < media_count; i++, m_ptr++) {
165 if (m_ptr->type_id == media_type)
166 break;
167 }
168
169 if ((i < media_count) && (m_ptr->addr2str != NULL)) {
170 char addr_str[MAX_ADDR_STR];
171
e91ed0bc 172 tipc_printf(pb, "%s(%s)", m_ptr->name,
b97bf3fd
PL
173 m_ptr->addr2str(a, addr_str, sizeof(addr_str)));
174 } else {
175 unchar *addr = (unchar *)&a->dev_addr;
176
e91ed0bc 177 tipc_printf(pb, "UNKNOWN(%u)", media_type);
a016892c 178 for (i = 0; i < (sizeof(*a) - sizeof(a->type)); i++)
e91ed0bc 179 tipc_printf(pb, "-%02x", addr[i]);
b97bf3fd
PL
180 }
181}
182
183/**
4323add6 184 * tipc_media_get_names - record names of registered media in buffer
b97bf3fd
PL
185 */
186
4323add6 187struct sk_buff *tipc_media_get_names(void)
b97bf3fd
PL
188{
189 struct sk_buff *buf;
190 struct media *m_ptr;
191 int i;
192
4323add6 193 buf = tipc_cfg_reply_alloc(MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME));
b97bf3fd
PL
194 if (!buf)
195 return NULL;
196
4323add6 197 read_lock_bh(&tipc_net_lock);
b97bf3fd 198 for (i = 0, m_ptr = media_list; i < media_count; i++, m_ptr++) {
c4307285 199 tipc_cfg_append_tlv(buf, TIPC_TLV_MEDIA_NAME, m_ptr->name,
4323add6 200 strlen(m_ptr->name) + 1);
b97bf3fd 201 }
4323add6 202 read_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
203 return buf;
204}
205
206/**
207 * bearer_name_validate - validate & (optionally) deconstruct bearer name
208 * @name - ptr to bearer name string
209 * @name_parts - ptr to area for bearer name components (or NULL if not needed)
c4307285 210 *
b97bf3fd
PL
211 * Returns 1 if bearer name is valid, otherwise 0.
212 */
213
c4307285 214static int bearer_name_validate(const char *name,
b97bf3fd
PL
215 struct bearer_name *name_parts)
216{
217 char name_copy[TIPC_MAX_BEARER_NAME];
218 char *media_name;
219 char *if_name;
220 u32 media_len;
221 u32 if_len;
222
223 /* copy bearer name & ensure length is OK */
224
225 name_copy[TIPC_MAX_BEARER_NAME - 1] = 0;
226 /* need above in case non-Posix strncpy() doesn't pad with nulls */
227 strncpy(name_copy, name, TIPC_MAX_BEARER_NAME);
228 if (name_copy[TIPC_MAX_BEARER_NAME - 1] != 0)
229 return 0;
230
231 /* ensure all component parts of bearer name are present */
232
233 media_name = name_copy;
2db9983a
AS
234 if_name = strchr(media_name, ':');
235 if (if_name == NULL)
b97bf3fd
PL
236 return 0;
237 *(if_name++) = 0;
238 media_len = if_name - media_name;
239 if_len = strlen(if_name) + 1;
240
241 /* validate component parts of bearer name */
242
c4307285
YH
243 if ((media_len <= 1) || (media_len > TIPC_MAX_MEDIA_NAME) ||
244 (if_len <= 1) || (if_len > TIPC_MAX_IF_NAME) ||
b97bf3fd
PL
245 (strspn(media_name, tipc_alphabet) != (media_len - 1)) ||
246 (strspn(if_name, tipc_alphabet) != (if_len - 1)))
247 return 0;
248
249 /* return bearer name components, if necessary */
250
251 if (name_parts) {
252 strcpy(name_parts->media_name, media_name);
253 strcpy(name_parts->if_name, if_name);
254 }
255 return 1;
256}
257
258/**
259 * bearer_find - locates bearer object with matching bearer name
260 */
261
2d627b92 262static struct tipc_bearer *bearer_find(const char *name)
b97bf3fd 263{
2d627b92 264 struct tipc_bearer *b_ptr;
b97bf3fd
PL
265 u32 i;
266
4323add6 267 for (i = 0, b_ptr = tipc_bearers; i < MAX_BEARERS; i++, b_ptr++) {
2d627b92 268 if (b_ptr->active && (!strcmp(b_ptr->name, name)))
b97bf3fd
PL
269 return b_ptr;
270 }
1fc54d8f 271 return NULL;
b97bf3fd
PL
272}
273
274/**
4323add6 275 * tipc_bearer_find_interface - locates bearer object with matching interface name
b97bf3fd
PL
276 */
277
2d627b92 278struct tipc_bearer *tipc_bearer_find_interface(const char *if_name)
b97bf3fd 279{
2d627b92 280 struct tipc_bearer *b_ptr;
b97bf3fd
PL
281 char *b_if_name;
282 u32 i;
283
4323add6 284 for (i = 0, b_ptr = tipc_bearers; i < MAX_BEARERS; i++, b_ptr++) {
b97bf3fd
PL
285 if (!b_ptr->active)
286 continue;
2d627b92 287 b_if_name = strchr(b_ptr->name, ':') + 1;
b97bf3fd
PL
288 if (!strcmp(b_if_name, if_name))
289 return b_ptr;
290 }
1fc54d8f 291 return NULL;
b97bf3fd
PL
292}
293
294/**
4323add6 295 * tipc_bearer_get_names - record names of bearers in buffer
b97bf3fd
PL
296 */
297
4323add6 298struct sk_buff *tipc_bearer_get_names(void)
b97bf3fd
PL
299{
300 struct sk_buff *buf;
301 struct media *m_ptr;
2d627b92 302 struct tipc_bearer *b_ptr;
b97bf3fd
PL
303 int i, j;
304
4323add6 305 buf = tipc_cfg_reply_alloc(MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME));
b97bf3fd
PL
306 if (!buf)
307 return NULL;
308
4323add6 309 read_lock_bh(&tipc_net_lock);
b97bf3fd
PL
310 for (i = 0, m_ptr = media_list; i < media_count; i++, m_ptr++) {
311 for (j = 0; j < MAX_BEARERS; j++) {
4323add6 312 b_ptr = &tipc_bearers[j];
b97bf3fd 313 if (b_ptr->active && (b_ptr->media == m_ptr)) {
c4307285 314 tipc_cfg_append_tlv(buf, TIPC_TLV_BEARER_NAME,
2d627b92
AS
315 b_ptr->name,
316 strlen(b_ptr->name) + 1);
b97bf3fd
PL
317 }
318 }
319 }
4323add6 320 read_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
321 return buf;
322}
323
2d627b92 324void tipc_bearer_add_dest(struct tipc_bearer *b_ptr, u32 dest)
b97bf3fd 325{
4323add6 326 tipc_nmap_add(&b_ptr->nodes, dest);
4323add6 327 tipc_bcbearer_sort();
1209966c 328 tipc_disc_add_dest(b_ptr->link_req);
b97bf3fd
PL
329}
330
2d627b92 331void tipc_bearer_remove_dest(struct tipc_bearer *b_ptr, u32 dest)
b97bf3fd 332{
4323add6 333 tipc_nmap_remove(&b_ptr->nodes, dest);
4323add6 334 tipc_bcbearer_sort();
1209966c 335 tipc_disc_remove_dest(b_ptr->link_req);
b97bf3fd
PL
336}
337
338/*
339 * bearer_push(): Resolve bearer congestion. Force the waiting
340 * links to push out their unsent packets, one packet per link
341 * per iteration, until all packets are gone or congestion reoccurs.
4323add6 342 * 'tipc_net_lock' is read_locked when this function is called
b97bf3fd
PL
343 * bearer.lock must be taken before calling
344 * Returns binary true(1) ore false(0)
345 */
2d627b92 346static int bearer_push(struct tipc_bearer *b_ptr)
b97bf3fd 347{
0e35fd5e 348 u32 res = 0;
b97bf3fd
PL
349 struct link *ln, *tln;
350
2d627b92 351 if (b_ptr->blocked)
b97bf3fd
PL
352 return 0;
353
354 while (!list_empty(&b_ptr->cong_links) && (res != PUSH_FAILED)) {
355 list_for_each_entry_safe(ln, tln, &b_ptr->cong_links, link_list) {
4323add6 356 res = tipc_link_push_packet(ln);
b97bf3fd
PL
357 if (res == PUSH_FAILED)
358 break;
359 if (res == PUSH_FINISHED)
360 list_move_tail(&ln->link_list, &b_ptr->links);
361 }
362 }
363 return list_empty(&b_ptr->cong_links);
364}
365
2d627b92 366void tipc_bearer_lock_push(struct tipc_bearer *b_ptr)
b97bf3fd 367{
2d627b92 368 spin_lock_bh(&b_ptr->lock);
23f0ff90 369 bearer_push(b_ptr);
2d627b92 370 spin_unlock_bh(&b_ptr->lock);
b97bf3fd
PL
371}
372
373
374/*
c4307285
YH
375 * Interrupt enabling new requests after bearer congestion or blocking:
376 * See bearer_send().
b97bf3fd 377 */
2d627b92 378void tipc_continue(struct tipc_bearer *b_ptr)
b97bf3fd 379{
2d627b92 380 spin_lock_bh(&b_ptr->lock);
b97bf3fd 381 if (!list_empty(&b_ptr->cong_links))
4323add6 382 tipc_k_signal((Handler)tipc_bearer_lock_push, (unsigned long)b_ptr);
2d627b92
AS
383 b_ptr->blocked = 0;
384 spin_unlock_bh(&b_ptr->lock);
b97bf3fd
PL
385}
386
387/*
c4307285
YH
388 * Schedule link for sending of messages after the bearer
389 * has been deblocked by 'continue()'. This method is called
390 * when somebody tries to send a message via this link while
4323add6 391 * the bearer is congested. 'tipc_net_lock' is in read_lock here
b97bf3fd
PL
392 * bearer.lock is busy
393 */
394
2d627b92 395static void tipc_bearer_schedule_unlocked(struct tipc_bearer *b_ptr, struct link *l_ptr)
b97bf3fd
PL
396{
397 list_move_tail(&l_ptr->link_list, &b_ptr->cong_links);
398}
399
400/*
c4307285
YH
401 * Schedule link for sending of messages after the bearer
402 * has been deblocked by 'continue()'. This method is called
403 * when somebody tries to send a message via this link while
4323add6 404 * the bearer is congested. 'tipc_net_lock' is in read_lock here,
b97bf3fd
PL
405 * bearer.lock is free
406 */
407
2d627b92 408void tipc_bearer_schedule(struct tipc_bearer *b_ptr, struct link *l_ptr)
b97bf3fd 409{
2d627b92 410 spin_lock_bh(&b_ptr->lock);
4323add6 411 tipc_bearer_schedule_unlocked(b_ptr, l_ptr);
2d627b92 412 spin_unlock_bh(&b_ptr->lock);
b97bf3fd
PL
413}
414
415
416/*
4323add6 417 * tipc_bearer_resolve_congestion(): Check if there is bearer congestion,
b97bf3fd 418 * and if there is, try to resolve it before returning.
4323add6 419 * 'tipc_net_lock' is read_locked when this function is called
b97bf3fd 420 */
2d627b92 421int tipc_bearer_resolve_congestion(struct tipc_bearer *b_ptr, struct link *l_ptr)
b97bf3fd
PL
422{
423 int res = 1;
424
425 if (list_empty(&b_ptr->cong_links))
426 return 1;
2d627b92 427 spin_lock_bh(&b_ptr->lock);
b97bf3fd 428 if (!bearer_push(b_ptr)) {
4323add6 429 tipc_bearer_schedule_unlocked(b_ptr, l_ptr);
b97bf3fd
PL
430 res = 0;
431 }
2d627b92 432 spin_unlock_bh(&b_ptr->lock);
b97bf3fd
PL
433 return res;
434}
435
b274f4ab
AS
436/**
437 * tipc_bearer_congested - determines if bearer is currently congested
438 */
439
2d627b92 440int tipc_bearer_congested(struct tipc_bearer *b_ptr, struct link *l_ptr)
b274f4ab 441{
2d627b92 442 if (unlikely(b_ptr->blocked))
b274f4ab
AS
443 return 1;
444 if (likely(list_empty(&b_ptr->cong_links)))
445 return 0;
446 return !tipc_bearer_resolve_congestion(b_ptr, l_ptr);
447}
b97bf3fd
PL
448
449/**
450 * tipc_enable_bearer - enable bearer with the given name
c4307285 451 */
b97bf3fd 452
50d3e639 453int tipc_enable_bearer(const char *name, u32 disc_domain, u32 priority)
b97bf3fd 454{
2d627b92 455 struct tipc_bearer *b_ptr;
b97bf3fd
PL
456 struct media *m_ptr;
457 struct bearer_name b_name;
458 char addr_string[16];
459 u32 bearer_id;
460 u32 with_this_prio;
461 u32 i;
462 int res = -EINVAL;
463
a10bd924
AS
464 if (tipc_mode != TIPC_NET_MODE) {
465 warn("Bearer <%s> rejected, not supported in standalone mode\n",
466 name);
b97bf3fd 467 return -ENOPROTOOPT;
a10bd924
AS
468 }
469 if (!bearer_name_validate(name, &b_name)) {
470 warn("Bearer <%s> rejected, illegal name\n", name);
16cb4b33 471 return -EINVAL;
a10bd924 472 }
66e019a6
AS
473 if (tipc_addr_domain_valid(disc_domain) &&
474 (disc_domain != tipc_own_addr)) {
475 if (tipc_in_scope(disc_domain, tipc_own_addr)) {
476 disc_domain = tipc_own_addr & TIPC_CLUSTER_MASK;
477 res = 0; /* accept any node in own cluster */
478 } else if (in_own_cluster(disc_domain))
479 res = 0; /* accept specified node in own cluster */
480 }
481 if (res) {
50d3e639 482 warn("Bearer <%s> rejected, illegal discovery domain\n", name);
a10bd924
AS
483 return -EINVAL;
484 }
16cb4b33
PL
485 if ((priority < TIPC_MIN_LINK_PRI ||
486 priority > TIPC_MAX_LINK_PRI) &&
a10bd924
AS
487 (priority != TIPC_MEDIA_LINK_PRI)) {
488 warn("Bearer <%s> rejected, illegal priority\n", name);
b97bf3fd 489 return -EINVAL;
a10bd924 490 }
b97bf3fd 491
4323add6 492 write_lock_bh(&tipc_net_lock);
b97bf3fd
PL
493
494 m_ptr = media_find(b_name.media_name);
495 if (!m_ptr) {
a10bd924
AS
496 warn("Bearer <%s> rejected, media <%s> not registered\n", name,
497 b_name.media_name);
3a777ff8 498 goto exit;
b97bf3fd 499 }
16cb4b33
PL
500
501 if (priority == TIPC_MEDIA_LINK_PRI)
b97bf3fd
PL
502 priority = m_ptr->priority;
503
504restart:
505 bearer_id = MAX_BEARERS;
506 with_this_prio = 1;
507 for (i = MAX_BEARERS; i-- != 0; ) {
4323add6 508 if (!tipc_bearers[i].active) {
b97bf3fd
PL
509 bearer_id = i;
510 continue;
511 }
2d627b92 512 if (!strcmp(name, tipc_bearers[i].name)) {
a10bd924 513 warn("Bearer <%s> rejected, already enabled\n", name);
3a777ff8 514 goto exit;
b97bf3fd 515 }
4323add6 516 if ((tipc_bearers[i].priority == priority) &&
b97bf3fd
PL
517 (++with_this_prio > 2)) {
518 if (priority-- == 0) {
a10bd924
AS
519 warn("Bearer <%s> rejected, duplicate priority\n",
520 name);
3a777ff8 521 goto exit;
b97bf3fd 522 }
a10bd924 523 warn("Bearer <%s> priority adjustment required %u->%u\n",
b97bf3fd
PL
524 name, priority + 1, priority);
525 goto restart;
526 }
527 }
528 if (bearer_id >= MAX_BEARERS) {
c4307285 529 warn("Bearer <%s> rejected, bearer limit reached (%u)\n",
a10bd924 530 name, MAX_BEARERS);
3a777ff8 531 goto exit;
b97bf3fd
PL
532 }
533
4323add6 534 b_ptr = &tipc_bearers[bearer_id];
2d627b92
AS
535 strcpy(b_ptr->name, name);
536 res = m_ptr->enable_bearer(b_ptr);
b97bf3fd 537 if (res) {
a10bd924 538 warn("Bearer <%s> rejected, enable failure (%d)\n", name, -res);
3a777ff8 539 goto exit;
b97bf3fd
PL
540 }
541
542 b_ptr->identity = bearer_id;
543 b_ptr->media = m_ptr;
544 b_ptr->net_plane = bearer_id + 'A';
545 b_ptr->active = 1;
b97bf3fd
PL
546 b_ptr->priority = priority;
547 INIT_LIST_HEAD(&b_ptr->cong_links);
548 INIT_LIST_HEAD(&b_ptr->links);
2d627b92 549 spin_lock_init(&b_ptr->lock);
3a777ff8
AS
550
551 res = tipc_disc_create(b_ptr, &m_ptr->bcast_addr, disc_domain);
552 if (res) {
553 bearer_disable(b_ptr);
554 warn("Bearer <%s> rejected, discovery object creation failed\n",
555 name);
556 goto exit;
557 }
16cb4b33 558 info("Enabled bearer <%s>, discovery domain %s, priority %u\n",
50d3e639 559 name, tipc_addr_string_fill(addr_string, disc_domain), priority);
3a777ff8 560exit:
4323add6 561 write_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
562 return res;
563}
564
565/**
566 * tipc_block_bearer(): Block the bearer with the given name,
567 * and reset all its links
568 */
569
570int tipc_block_bearer(const char *name)
571{
2d627b92 572 struct tipc_bearer *b_ptr = NULL;
b97bf3fd
PL
573 struct link *l_ptr;
574 struct link *temp_l_ptr;
575
4323add6 576 read_lock_bh(&tipc_net_lock);
b97bf3fd
PL
577 b_ptr = bearer_find(name);
578 if (!b_ptr) {
579 warn("Attempt to block unknown bearer <%s>\n", name);
4323add6 580 read_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
581 return -EINVAL;
582 }
583
a10bd924 584 info("Blocking bearer <%s>\n", name);
2d627b92
AS
585 spin_lock_bh(&b_ptr->lock);
586 b_ptr->blocked = 1;
4b3743ef 587 list_splice_init(&b_ptr->cong_links, &b_ptr->links);
b97bf3fd 588 list_for_each_entry_safe(l_ptr, temp_l_ptr, &b_ptr->links, link_list) {
6c00055a 589 struct tipc_node *n_ptr = l_ptr->owner;
b97bf3fd
PL
590
591 spin_lock_bh(&n_ptr->lock);
4323add6 592 tipc_link_reset(l_ptr);
b97bf3fd
PL
593 spin_unlock_bh(&n_ptr->lock);
594 }
2d627b92 595 spin_unlock_bh(&b_ptr->lock);
4323add6 596 read_unlock_bh(&tipc_net_lock);
0e35fd5e 597 return 0;
b97bf3fd
PL
598}
599
600/**
601 * bearer_disable -
c4307285 602 *
4323add6 603 * Note: This routine assumes caller holds tipc_net_lock.
b97bf3fd
PL
604 */
605
2d627b92 606static void bearer_disable(struct tipc_bearer *b_ptr)
b97bf3fd 607{
b97bf3fd
PL
608 struct link *l_ptr;
609 struct link *temp_l_ptr;
610
2d627b92 611 info("Disabling bearer <%s>\n", b_ptr->name);
2d627b92 612 spin_lock_bh(&b_ptr->lock);
2d627b92
AS
613 b_ptr->blocked = 1;
614 b_ptr->media->disable_bearer(b_ptr);
4b3743ef 615 list_splice_init(&b_ptr->cong_links, &b_ptr->links);
b97bf3fd 616 list_for_each_entry_safe(l_ptr, temp_l_ptr, &b_ptr->links, link_list) {
4323add6 617 tipc_link_delete(l_ptr);
b97bf3fd 618 }
3a777ff8
AS
619 if (b_ptr->link_req)
620 tipc_disc_delete(b_ptr->link_req);
2d627b92
AS
621 spin_unlock_bh(&b_ptr->lock);
622 memset(b_ptr, 0, sizeof(struct tipc_bearer));
b97bf3fd
PL
623}
624
625int tipc_disable_bearer(const char *name)
626{
2d627b92 627 struct tipc_bearer *b_ptr;
b97bf3fd
PL
628 int res;
629
4323add6 630 write_lock_bh(&tipc_net_lock);
ccc901ee
AS
631 b_ptr = bearer_find(name);
632 if (b_ptr == NULL) {
633 warn("Attempt to disable unknown bearer <%s>\n", name);
634 res = -EINVAL;
28cc937e
AS
635 } else {
636 bearer_disable(b_ptr);
637 res = 0;
638 }
4323add6 639 write_unlock_bh(&tipc_net_lock);
b97bf3fd
PL
640 return res;
641}
642
643
644
4323add6 645void tipc_bearer_stop(void)
b97bf3fd
PL
646{
647 u32 i;
648
b97bf3fd 649 for (i = 0; i < MAX_BEARERS; i++) {
4323add6 650 if (tipc_bearers[i].active)
ccc901ee 651 bearer_disable(&tipc_bearers[i]);
b97bf3fd 652 }
b97bf3fd
PL
653 media_count = 0;
654}