9ff89207846ab87bf160717b96498ab5757e7fa6
2 * net/tipc/bearer.c: TIPC bearer code
4 * Copyright (c) 1996-2006, Ericsson AB
5 * Copyright (c) 2004-2006, 2010-2011, Wind River Systems
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.
42 #define MAX_ADDR_STR 32
44 static struct media media_list
[MAX_MEDIA
];
45 static u32 media_count
;
47 struct tipc_bearer tipc_bearers
[MAX_BEARERS
];
49 static void bearer_disable(struct tipc_bearer
*b_ptr
);
52 * media_name_valid - validate media name
54 * Returns 1 if media name is valid, otherwise 0.
57 static int media_name_valid(const char *name
)
62 if ((len
+ 1) > TIPC_MAX_MEDIA_NAME
)
64 return strspn(name
, tipc_alphabet
) == len
;
68 * media_find - locates specified media object by name
71 static struct media
*media_find(const char *name
)
76 for (i
= 0, m_ptr
= media_list
; i
< media_count
; i
++, m_ptr
++) {
77 if (!strcmp(m_ptr
->name
, name
))
84 * media_find_id - locates specified media object by type identifier
87 static struct media
*media_find_id(u8 type
)
91 for (i
= 0; i
< media_count
; i
++) {
92 if (media_list
[i
].type_id
== type
)
93 return &media_list
[i
];
99 * tipc_register_media - register a media type
101 * Bearers for this media type must be activated separately at a later stage.
104 int tipc_register_media(struct media
*m_ptr
)
108 write_lock_bh(&tipc_net_lock
);
110 if (tipc_mode
!= TIPC_NET_MODE
) {
111 warn("Media <%s> rejected, not in networked mode yet\n",
115 if (!media_name_valid(m_ptr
->name
)) {
116 warn("Media <%s> rejected, illegal name\n", m_ptr
->name
);
119 if (m_ptr
->bcast_addr
.type
!= htonl(m_ptr
->type_id
)) {
120 warn("Media <%s> rejected, illegal broadcast address\n",
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
);
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
);
137 if (media_count
>= MAX_MEDIA
) {
138 warn("Media <%s> rejected, media limit reached (%u)\n",
139 m_ptr
->name
, MAX_MEDIA
);
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
);
147 media_list
[media_count
] = *m_ptr
;
151 write_unlock_bh(&tipc_net_lock
);
156 * tipc_media_addr_printf - record media address in print buffer
159 void tipc_media_addr_printf(struct print_buf
*pb
, struct tipc_media_addr
*a
)
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
)
171 if ((i
< media_count
) && (m_ptr
->addr2str
!= NULL
)) {
172 char addr_str
[MAX_ADDR_STR
];
174 tipc_printf(pb
, "%s(%s)", m_ptr
->name
,
175 m_ptr
->addr2str(a
, addr_str
, sizeof(addr_str
)));
177 unchar
*addr
= (unchar
*)&a
->dev_addr
;
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
]);
186 * tipc_media_get_names - record names of registered media in buffer
189 struct sk_buff
*tipc_media_get_names(void)
195 buf
= tipc_cfg_reply_alloc(MAX_MEDIA
* TLV_SPACE(TIPC_MAX_MEDIA_NAME
));
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);
204 read_unlock_bh(&tipc_net_lock
);
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)
213 * Returns 1 if bearer name is valid, otherwise 0.
216 static int bearer_name_validate(const char *name
,
217 struct bearer_name
*name_parts
)
219 char name_copy
[TIPC_MAX_BEARER_NAME
];
225 /* copy bearer name & ensure length is OK */
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)
233 /* ensure all component parts of bearer name are present */
235 media_name
= name_copy
;
236 if_name
= strchr(media_name
, ':');
240 media_len
= if_name
- media_name
;
241 if_len
= strlen(if_name
) + 1;
243 /* validate component parts of bearer name */
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)))
251 /* return bearer name components, if necessary */
254 strcpy(name_parts
->media_name
, media_name
);
255 strcpy(name_parts
->if_name
, if_name
);
261 * bearer_find - locates bearer object with matching bearer name
264 static struct tipc_bearer
*bearer_find(const char *name
)
266 struct tipc_bearer
*b_ptr
;
269 for (i
= 0, b_ptr
= tipc_bearers
; i
< MAX_BEARERS
; i
++, b_ptr
++) {
270 if (b_ptr
->active
&& (!strcmp(b_ptr
->name
, name
)))
277 * tipc_bearer_find_interface - locates bearer object with matching interface name
280 struct tipc_bearer
*tipc_bearer_find_interface(const char *if_name
)
282 struct tipc_bearer
*b_ptr
;
286 for (i
= 0, b_ptr
= tipc_bearers
; i
< MAX_BEARERS
; i
++, b_ptr
++) {
289 b_if_name
= strchr(b_ptr
->name
, ':') + 1;
290 if (!strcmp(b_if_name
, if_name
))
297 * tipc_bearer_get_names - record names of bearers in buffer
300 struct sk_buff
*tipc_bearer_get_names(void)
304 struct tipc_bearer
*b_ptr
;
307 buf
= tipc_cfg_reply_alloc(MAX_BEARERS
* TLV_SPACE(TIPC_MAX_BEARER_NAME
));
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
,
318 strlen(b_ptr
->name
) + 1);
322 read_unlock_bh(&tipc_net_lock
);
326 void tipc_bearer_add_dest(struct tipc_bearer
*b_ptr
, u32 dest
)
328 tipc_nmap_add(&b_ptr
->nodes
, dest
);
329 tipc_bcbearer_sort();
330 tipc_disc_add_dest(b_ptr
->link_req
);
333 void tipc_bearer_remove_dest(struct tipc_bearer
*b_ptr
, u32 dest
)
335 tipc_nmap_remove(&b_ptr
->nodes
, dest
);
336 tipc_bcbearer_sort();
337 tipc_disc_remove_dest(b_ptr
->link_req
);
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)
348 static int bearer_push(struct tipc_bearer
*b_ptr
)
351 struct link
*ln
, *tln
;
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
)
361 if (res
== PUSH_FINISHED
)
362 list_move_tail(&ln
->link_list
, &b_ptr
->links
);
365 return list_empty(&b_ptr
->cong_links
);
368 void tipc_bearer_lock_push(struct tipc_bearer
*b_ptr
)
370 spin_lock_bh(&b_ptr
->lock
);
372 spin_unlock_bh(&b_ptr
->lock
);
377 * Interrupt enabling new requests after bearer congestion or blocking:
380 void tipc_continue(struct tipc_bearer
*b_ptr
)
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
);
386 spin_unlock_bh(&b_ptr
->lock
);
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
397 static void tipc_bearer_schedule_unlocked(struct tipc_bearer
*b_ptr
, struct link
*l_ptr
)
399 list_move_tail(&l_ptr
->link_list
, &b_ptr
->cong_links
);
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
410 void tipc_bearer_schedule(struct tipc_bearer
*b_ptr
, struct link
*l_ptr
)
412 spin_lock_bh(&b_ptr
->lock
);
413 tipc_bearer_schedule_unlocked(b_ptr
, l_ptr
);
414 spin_unlock_bh(&b_ptr
->lock
);
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
423 int tipc_bearer_resolve_congestion(struct tipc_bearer
*b_ptr
, struct link
*l_ptr
)
427 if (list_empty(&b_ptr
->cong_links
))
429 spin_lock_bh(&b_ptr
->lock
);
430 if (!bearer_push(b_ptr
)) {
431 tipc_bearer_schedule_unlocked(b_ptr
, l_ptr
);
434 spin_unlock_bh(&b_ptr
->lock
);
439 * tipc_bearer_congested - determines if bearer is currently congested
442 int tipc_bearer_congested(struct tipc_bearer
*b_ptr
, struct link
*l_ptr
)
444 if (unlikely(b_ptr
->blocked
))
446 if (likely(list_empty(&b_ptr
->cong_links
)))
448 return !tipc_bearer_resolve_congestion(b_ptr
, l_ptr
);
452 * tipc_enable_bearer - enable bearer with the given name
455 int tipc_enable_bearer(const char *name
, u32 disc_domain
, u32 priority
)
457 struct tipc_bearer
*b_ptr
;
459 struct bearer_name b_name
;
460 char addr_string
[16];
466 if (tipc_mode
!= TIPC_NET_MODE
) {
467 warn("Bearer <%s> rejected, not supported in standalone mode\n",
471 if (!bearer_name_validate(name
, &b_name
)) {
472 warn("Bearer <%s> rejected, illegal name\n", name
);
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 */
484 warn("Bearer <%s> rejected, illegal discovery domain\n", name
);
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
);
494 write_lock_bh(&tipc_net_lock
);
496 m_ptr
= media_find(b_name
.media_name
);
498 warn("Bearer <%s> rejected, media <%s> not registered\n", name
,
503 if (priority
== TIPC_MEDIA_LINK_PRI
)
504 priority
= m_ptr
->priority
;
507 bearer_id
= MAX_BEARERS
;
509 for (i
= MAX_BEARERS
; i
-- != 0; ) {
510 if (!tipc_bearers
[i
].active
) {
514 if (!strcmp(name
, tipc_bearers
[i
].name
)) {
515 warn("Bearer <%s> rejected, already enabled\n", name
);
518 if ((tipc_bearers
[i
].priority
== priority
) &&
519 (++with_this_prio
> 2)) {
520 if (priority
-- == 0) {
521 warn("Bearer <%s> rejected, duplicate priority\n",
525 warn("Bearer <%s> priority adjustment required %u->%u\n",
526 name
, priority
+ 1, priority
);
530 if (bearer_id
>= MAX_BEARERS
) {
531 warn("Bearer <%s> rejected, bearer limit reached (%u)\n",
536 b_ptr
= &tipc_bearers
[bearer_id
];
537 strcpy(b_ptr
->name
, name
);
538 res
= m_ptr
->enable_bearer(b_ptr
);
540 warn("Bearer <%s> rejected, enable failure (%d)\n", name
, -res
);
544 b_ptr
->identity
= bearer_id
;
545 b_ptr
->media
= m_ptr
;
546 b_ptr
->net_plane
= bearer_id
+ 'A';
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
);
553 res
= tipc_disc_create(b_ptr
, &m_ptr
->bcast_addr
, disc_domain
);
555 bearer_disable(b_ptr
);
556 warn("Bearer <%s> rejected, discovery object creation failed\n",
560 info("Enabled bearer <%s>, discovery domain %s, priority %u\n",
561 name
, tipc_addr_string_fill(addr_string
, disc_domain
), priority
);
563 write_unlock_bh(&tipc_net_lock
);
568 * tipc_block_bearer(): Block the bearer with the given name,
569 * and reset all its links
572 int tipc_block_bearer(const char *name
)
574 struct tipc_bearer
*b_ptr
= NULL
;
576 struct link
*temp_l_ptr
;
578 read_lock_bh(&tipc_net_lock
);
579 b_ptr
= bearer_find(name
);
581 warn("Attempt to block unknown bearer <%s>\n", name
);
582 read_unlock_bh(&tipc_net_lock
);
586 info("Blocking bearer <%s>\n", name
);
587 spin_lock_bh(&b_ptr
->lock
);
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
;
593 spin_lock_bh(&n_ptr
->lock
);
594 tipc_link_reset(l_ptr
);
595 spin_unlock_bh(&n_ptr
->lock
);
597 spin_unlock_bh(&b_ptr
->lock
);
598 read_unlock_bh(&tipc_net_lock
);
605 * Note: This routine assumes caller holds tipc_net_lock.
608 static void bearer_disable(struct tipc_bearer
*b_ptr
)
611 struct link
*temp_l_ptr
;
613 info("Disabling bearer <%s>\n", b_ptr
->name
);
614 spin_lock_bh(&b_ptr
->lock
);
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
);
622 tipc_disc_delete(b_ptr
->link_req
);
623 spin_unlock_bh(&b_ptr
->lock
);
624 memset(b_ptr
, 0, sizeof(struct tipc_bearer
));
627 int tipc_disable_bearer(const char *name
)
629 struct tipc_bearer
*b_ptr
;
632 write_lock_bh(&tipc_net_lock
);
633 b_ptr
= bearer_find(name
);
635 warn("Attempt to disable unknown bearer <%s>\n", name
);
638 bearer_disable(b_ptr
);
641 write_unlock_bh(&tipc_net_lock
);
647 void tipc_bearer_stop(void)
651 for (i
= 0; i
< MAX_BEARERS
; i
++) {
652 if (tipc_bearers
[i
].active
)
653 bearer_disable(&tipc_bearers
[i
]);
This page took 0.041842 seconds and 4 git commands to generate.