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