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 tipc_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.
56 static int media_name_valid(const char *name
)
61 if ((len
+ 1) > TIPC_MAX_MEDIA_NAME
)
63 return strspn(name
, tipc_alphabet
) == len
;
67 * tipc_media_find - locates specified media object by name
69 struct tipc_media
*tipc_media_find(const char *name
)
73 for (i
= 0; i
< media_count
; i
++) {
74 if (!strcmp(media_list
[i
]->name
, name
))
81 * media_find_id - locates specified media object by type identifier
83 static struct tipc_media
*media_find_id(u8 type
)
87 for (i
= 0; i
< media_count
; i
++) {
88 if (media_list
[i
]->type_id
== type
)
95 * tipc_register_media - register a media type
97 * Bearers for this media type must be activated separately at a later stage.
99 int tipc_register_media(struct tipc_media
*m_ptr
)
103 write_lock_bh(&tipc_net_lock
);
105 if (!media_name_valid(m_ptr
->name
))
107 if ((m_ptr
->bcast_addr
.media_id
!= m_ptr
->type_id
) ||
108 !m_ptr
->bcast_addr
.broadcast
)
110 if (m_ptr
->priority
> TIPC_MAX_LINK_PRI
)
112 if ((m_ptr
->tolerance
< TIPC_MIN_LINK_TOL
) ||
113 (m_ptr
->tolerance
> TIPC_MAX_LINK_TOL
))
115 if (media_count
>= MAX_MEDIA
)
117 if (tipc_media_find(m_ptr
->name
) || media_find_id(m_ptr
->type_id
))
120 media_list
[media_count
] = m_ptr
;
124 write_unlock_bh(&tipc_net_lock
);
126 pr_warn("Media <%s> registration error\n", m_ptr
->name
);
131 * tipc_media_addr_printf - record media address in print buffer
133 void tipc_media_addr_printf(char *buf
, int len
, struct tipc_media_addr
*a
)
135 char addr_str
[MAX_ADDR_STR
];
136 struct tipc_media
*m_ptr
;
139 m_ptr
= media_find_id(a
->media_id
);
141 if (m_ptr
&& !m_ptr
->addr2str(a
, addr_str
, sizeof(addr_str
)))
142 ret
= tipc_snprintf(buf
, len
, "%s(%s)", m_ptr
->name
, addr_str
);
146 ret
= tipc_snprintf(buf
, len
, "UNKNOWN(%u)", a
->media_id
);
147 for (i
= 0; i
< sizeof(a
->value
); i
++)
148 ret
+= tipc_snprintf(buf
- ret
, len
+ ret
,
149 "-%02x", a
->value
[i
]);
154 * tipc_media_get_names - record names of registered media in buffer
156 struct sk_buff
*tipc_media_get_names(void)
161 buf
= tipc_cfg_reply_alloc(MAX_MEDIA
* TLV_SPACE(TIPC_MAX_MEDIA_NAME
));
165 read_lock_bh(&tipc_net_lock
);
166 for (i
= 0; i
< media_count
; i
++) {
167 tipc_cfg_append_tlv(buf
, TIPC_TLV_MEDIA_NAME
,
169 strlen(media_list
[i
]->name
) + 1);
171 read_unlock_bh(&tipc_net_lock
);
176 * bearer_name_validate - validate & (optionally) deconstruct bearer name
177 * @name: ptr to bearer name string
178 * @name_parts: ptr to area for bearer name components (or NULL if not needed)
180 * Returns 1 if bearer name is valid, otherwise 0.
182 static int bearer_name_validate(const char *name
,
183 struct tipc_bearer_names
*name_parts
)
185 char name_copy
[TIPC_MAX_BEARER_NAME
];
191 /* copy bearer name & ensure length is OK */
192 name_copy
[TIPC_MAX_BEARER_NAME
- 1] = 0;
193 /* need above in case non-Posix strncpy() doesn't pad with nulls */
194 strncpy(name_copy
, name
, TIPC_MAX_BEARER_NAME
);
195 if (name_copy
[TIPC_MAX_BEARER_NAME
- 1] != 0)
198 /* ensure all component parts of bearer name are present */
199 media_name
= name_copy
;
200 if_name
= strchr(media_name
, ':');
204 media_len
= if_name
- media_name
;
205 if_len
= strlen(if_name
) + 1;
207 /* validate component parts of bearer name */
208 if ((media_len
<= 1) || (media_len
> TIPC_MAX_MEDIA_NAME
) ||
209 (if_len
<= 1) || (if_len
> TIPC_MAX_IF_NAME
) ||
210 (strspn(media_name
, tipc_alphabet
) != (media_len
- 1)) ||
211 (strspn(if_name
, tipc_alphabet
) != (if_len
- 1)))
214 /* return bearer name components, if necessary */
216 strcpy(name_parts
->media_name
, media_name
);
217 strcpy(name_parts
->if_name
, if_name
);
223 * tipc_bearer_find - locates bearer object with matching bearer name
225 struct tipc_bearer
*tipc_bearer_find(const char *name
)
227 struct tipc_bearer
*b_ptr
;
230 for (i
= 0, b_ptr
= tipc_bearers
; i
< MAX_BEARERS
; i
++, b_ptr
++) {
231 if (b_ptr
->active
&& (!strcmp(b_ptr
->name
, name
)))
238 * tipc_bearer_find_interface - locates bearer object with matching interface name
240 struct tipc_bearer
*tipc_bearer_find_interface(const char *if_name
)
242 struct tipc_bearer
*b_ptr
;
246 for (i
= 0, b_ptr
= tipc_bearers
; i
< MAX_BEARERS
; i
++, b_ptr
++) {
249 b_if_name
= strchr(b_ptr
->name
, ':') + 1;
250 if (!strcmp(b_if_name
, if_name
))
257 * tipc_bearer_get_names - record names of bearers in buffer
259 struct sk_buff
*tipc_bearer_get_names(void)
262 struct tipc_bearer
*b_ptr
;
265 buf
= tipc_cfg_reply_alloc(MAX_BEARERS
* TLV_SPACE(TIPC_MAX_BEARER_NAME
));
269 read_lock_bh(&tipc_net_lock
);
270 for (i
= 0; i
< media_count
; i
++) {
271 for (j
= 0; j
< MAX_BEARERS
; j
++) {
272 b_ptr
= &tipc_bearers
[j
];
273 if (b_ptr
->active
&& (b_ptr
->media
== media_list
[i
])) {
274 tipc_cfg_append_tlv(buf
, TIPC_TLV_BEARER_NAME
,
276 strlen(b_ptr
->name
) + 1);
280 read_unlock_bh(&tipc_net_lock
);
284 void tipc_bearer_add_dest(struct tipc_bearer
*b_ptr
, u32 dest
)
286 tipc_nmap_add(&b_ptr
->nodes
, dest
);
287 tipc_bcbearer_sort();
288 tipc_disc_add_dest(b_ptr
->link_req
);
291 void tipc_bearer_remove_dest(struct tipc_bearer
*b_ptr
, u32 dest
)
293 tipc_nmap_remove(&b_ptr
->nodes
, dest
);
294 tipc_bcbearer_sort();
295 tipc_disc_remove_dest(b_ptr
->link_req
);
299 * bearer_push(): Resolve bearer congestion. Force the waiting
300 * links to push out their unsent packets, one packet per link
301 * per iteration, until all packets are gone or congestion reoccurs.
302 * 'tipc_net_lock' is read_locked when this function is called
303 * bearer.lock must be taken before calling
304 * Returns binary true(1) ore false(0)
306 static int bearer_push(struct tipc_bearer
*b_ptr
)
309 struct tipc_link
*ln
, *tln
;
314 while (!list_empty(&b_ptr
->cong_links
) && (res
!= PUSH_FAILED
)) {
315 list_for_each_entry_safe(ln
, tln
, &b_ptr
->cong_links
, link_list
) {
316 res
= tipc_link_push_packet(ln
);
317 if (res
== PUSH_FAILED
)
319 if (res
== PUSH_FINISHED
)
320 list_move_tail(&ln
->link_list
, &b_ptr
->links
);
323 return list_empty(&b_ptr
->cong_links
);
326 void tipc_bearer_lock_push(struct tipc_bearer
*b_ptr
)
328 spin_lock_bh(&b_ptr
->lock
);
330 spin_unlock_bh(&b_ptr
->lock
);
335 * Interrupt enabling new requests after bearer congestion or blocking:
338 void tipc_continue(struct tipc_bearer
*b_ptr
)
340 spin_lock_bh(&b_ptr
->lock
);
341 if (!list_empty(&b_ptr
->cong_links
))
342 tipc_k_signal((Handler
)tipc_bearer_lock_push
, (unsigned long)b_ptr
);
344 spin_unlock_bh(&b_ptr
->lock
);
348 * Schedule link for sending of messages after the bearer
349 * has been deblocked by 'continue()'. This method is called
350 * when somebody tries to send a message via this link while
351 * the bearer is congested. 'tipc_net_lock' is in read_lock here
352 * bearer.lock is busy
354 static void tipc_bearer_schedule_unlocked(struct tipc_bearer
*b_ptr
,
355 struct tipc_link
*l_ptr
)
357 list_move_tail(&l_ptr
->link_list
, &b_ptr
->cong_links
);
361 * Schedule link for sending of messages after the bearer
362 * has been deblocked by 'continue()'. This method is called
363 * when somebody tries to send a message via this link while
364 * the bearer is congested. 'tipc_net_lock' is in read_lock here,
365 * bearer.lock is free
367 void tipc_bearer_schedule(struct tipc_bearer
*b_ptr
, struct tipc_link
*l_ptr
)
369 spin_lock_bh(&b_ptr
->lock
);
370 tipc_bearer_schedule_unlocked(b_ptr
, l_ptr
);
371 spin_unlock_bh(&b_ptr
->lock
);
376 * tipc_bearer_resolve_congestion(): Check if there is bearer congestion,
377 * and if there is, try to resolve it before returning.
378 * 'tipc_net_lock' is read_locked when this function is called
380 int tipc_bearer_resolve_congestion(struct tipc_bearer
*b_ptr
,
381 struct tipc_link
*l_ptr
)
385 if (list_empty(&b_ptr
->cong_links
))
387 spin_lock_bh(&b_ptr
->lock
);
388 if (!bearer_push(b_ptr
)) {
389 tipc_bearer_schedule_unlocked(b_ptr
, l_ptr
);
392 spin_unlock_bh(&b_ptr
->lock
);
397 * tipc_bearer_congested - determines if bearer is currently congested
399 int tipc_bearer_congested(struct tipc_bearer
*b_ptr
, struct tipc_link
*l_ptr
)
401 if (unlikely(b_ptr
->blocked
))
403 if (likely(list_empty(&b_ptr
->cong_links
)))
405 return !tipc_bearer_resolve_congestion(b_ptr
, l_ptr
);
409 * tipc_enable_bearer - enable bearer with the given name
411 int tipc_enable_bearer(const char *name
, u32 disc_domain
, u32 priority
)
413 struct tipc_bearer
*b_ptr
;
414 struct tipc_media
*m_ptr
;
415 struct tipc_bearer_names b_names
;
416 char addr_string
[16];
422 if (!tipc_own_addr
) {
423 pr_warn("Bearer <%s> rejected, not supported in standalone mode\n",
427 if (!bearer_name_validate(name
, &b_names
)) {
428 pr_warn("Bearer <%s> rejected, illegal name\n", name
);
431 if (tipc_addr_domain_valid(disc_domain
) &&
432 (disc_domain
!= tipc_own_addr
)) {
433 if (tipc_in_scope(disc_domain
, tipc_own_addr
)) {
434 disc_domain
= tipc_own_addr
& TIPC_CLUSTER_MASK
;
435 res
= 0; /* accept any node in own cluster */
436 } else if (in_own_cluster_exact(disc_domain
))
437 res
= 0; /* accept specified node in own cluster */
440 pr_warn("Bearer <%s> rejected, illegal discovery domain\n",
444 if ((priority
> TIPC_MAX_LINK_PRI
) &&
445 (priority
!= TIPC_MEDIA_LINK_PRI
)) {
446 pr_warn("Bearer <%s> rejected, illegal priority\n", name
);
450 write_lock_bh(&tipc_net_lock
);
452 m_ptr
= tipc_media_find(b_names
.media_name
);
454 pr_warn("Bearer <%s> rejected, media <%s> not registered\n",
455 name
, b_names
.media_name
);
459 if (priority
== TIPC_MEDIA_LINK_PRI
)
460 priority
= m_ptr
->priority
;
463 bearer_id
= MAX_BEARERS
;
465 for (i
= MAX_BEARERS
; i
-- != 0; ) {
466 if (!tipc_bearers
[i
].active
) {
470 if (!strcmp(name
, tipc_bearers
[i
].name
)) {
471 pr_warn("Bearer <%s> rejected, already enabled\n",
475 if ((tipc_bearers
[i
].priority
== priority
) &&
476 (++with_this_prio
> 2)) {
477 if (priority
-- == 0) {
478 pr_warn("Bearer <%s> rejected, duplicate priority\n",
482 pr_warn("Bearer <%s> priority adjustment required %u->%u\n",
483 name
, priority
+ 1, priority
);
487 if (bearer_id
>= MAX_BEARERS
) {
488 pr_warn("Bearer <%s> rejected, bearer limit reached (%u)\n",
493 b_ptr
= &tipc_bearers
[bearer_id
];
494 strcpy(b_ptr
->name
, name
);
495 res
= m_ptr
->enable_bearer(b_ptr
);
497 pr_warn("Bearer <%s> rejected, enable failure (%d)\n",
502 b_ptr
->identity
= bearer_id
;
503 b_ptr
->media
= m_ptr
;
504 b_ptr
->tolerance
= m_ptr
->tolerance
;
505 b_ptr
->window
= m_ptr
->window
;
506 b_ptr
->net_plane
= bearer_id
+ 'A';
508 b_ptr
->priority
= priority
;
509 INIT_LIST_HEAD(&b_ptr
->cong_links
);
510 INIT_LIST_HEAD(&b_ptr
->links
);
511 spin_lock_init(&b_ptr
->lock
);
513 res
= tipc_disc_create(b_ptr
, &m_ptr
->bcast_addr
, disc_domain
);
515 bearer_disable(b_ptr
);
516 pr_warn("Bearer <%s> rejected, discovery object creation failed\n",
520 pr_info("Enabled bearer <%s>, discovery domain %s, priority %u\n",
522 tipc_addr_string_fill(addr_string
, disc_domain
), priority
);
524 write_unlock_bh(&tipc_net_lock
);
529 * tipc_block_bearer - Block the bearer with the given name, and reset all its links
531 int tipc_block_bearer(const char *name
)
533 struct tipc_bearer
*b_ptr
= NULL
;
534 struct tipc_link
*l_ptr
;
535 struct tipc_link
*temp_l_ptr
;
537 read_lock_bh(&tipc_net_lock
);
538 b_ptr
= tipc_bearer_find(name
);
540 pr_warn("Attempt to block unknown bearer <%s>\n", name
);
541 read_unlock_bh(&tipc_net_lock
);
545 pr_info("Blocking bearer <%s>\n", name
);
546 spin_lock_bh(&b_ptr
->lock
);
548 list_splice_init(&b_ptr
->cong_links
, &b_ptr
->links
);
549 list_for_each_entry_safe(l_ptr
, temp_l_ptr
, &b_ptr
->links
, link_list
) {
550 struct tipc_node
*n_ptr
= l_ptr
->owner
;
552 spin_lock_bh(&n_ptr
->lock
);
553 tipc_link_reset(l_ptr
);
554 spin_unlock_bh(&n_ptr
->lock
);
556 spin_unlock_bh(&b_ptr
->lock
);
557 read_unlock_bh(&tipc_net_lock
);
564 * Note: This routine assumes caller holds tipc_net_lock.
566 static void bearer_disable(struct tipc_bearer
*b_ptr
)
568 struct tipc_link
*l_ptr
;
569 struct tipc_link
*temp_l_ptr
;
571 pr_info("Disabling bearer <%s>\n", b_ptr
->name
);
572 spin_lock_bh(&b_ptr
->lock
);
574 b_ptr
->media
->disable_bearer(b_ptr
);
575 list_splice_init(&b_ptr
->cong_links
, &b_ptr
->links
);
576 list_for_each_entry_safe(l_ptr
, temp_l_ptr
, &b_ptr
->links
, link_list
) {
577 tipc_link_delete(l_ptr
);
580 tipc_disc_delete(b_ptr
->link_req
);
581 spin_unlock_bh(&b_ptr
->lock
);
582 memset(b_ptr
, 0, sizeof(struct tipc_bearer
));
585 int tipc_disable_bearer(const char *name
)
587 struct tipc_bearer
*b_ptr
;
590 write_lock_bh(&tipc_net_lock
);
591 b_ptr
= tipc_bearer_find(name
);
593 pr_warn("Attempt to disable unknown bearer <%s>\n", name
);
596 bearer_disable(b_ptr
);
599 write_unlock_bh(&tipc_net_lock
);
605 void tipc_bearer_stop(void)
609 for (i
= 0; i
< MAX_BEARERS
; i
++) {
610 if (tipc_bearers
[i
].active
)
611 bearer_disable(&tipc_bearers
[i
]);