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