2 * net/tipc/bearer.c: TIPC bearer code
4 * Copyright (c) 2003-2005, Ericsson Research Canada
5 * Copyright (c) 2004-2005, Wind River Systems
6 * Copyright (c) 2005-2006, Ericsson AB
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 * list of conditions and the following disclaimer.
14 * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * Neither the names of the copyright holders nor the names of its
18 * contributors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
43 #define MAX_ADDR_STR 32
45 static struct media *media_list = 0;
46 static u32 media_count = 0;
48 struct bearer *bearers = 0;
51 * media_name_valid - validate media name
53 * 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 * media_find - locates specified media object by name
70 static struct media *media_find(const char *name)
75 for (i = 0, m_ptr = media_list; i < media_count; i++, m_ptr++) {
76 if (!strcmp(m_ptr->name, name))
83 * tipc_register_media - register a media type
85 * Bearers for this media type must be activated separately at a later stage.
88 int tipc_register_media(u32 media_type,
90 int (*enable)(struct tipc_bearer *),
91 void (*disable)(struct tipc_bearer *),
92 int (*send_msg)(struct sk_buff *,
94 struct tipc_media_addr *),
95 char *(*addr2str)(struct tipc_media_addr *a,
96 char *str_buf, int str_size),
97 struct tipc_media_addr *bcast_addr,
98 const u32 bearer_priority,
99 const u32 link_tolerance, /* [ms] */
100 const u32 send_window_limit)
107 write_lock_bh(&net_lock);
111 if (!media_name_valid(name)) {
112 warn("Media registration error: illegal name <%s>\n", name);
116 warn("Media registration error: no broadcast address supplied\n");
119 if (bearer_priority >= TIPC_NUM_LINK_PRI) {
120 warn("Media registration error: priority %u\n", bearer_priority);
123 if ((link_tolerance < TIPC_MIN_LINK_TOL) ||
124 (link_tolerance > TIPC_MAX_LINK_TOL)) {
125 warn("Media registration error: tolerance %u\n", link_tolerance);
129 media_id = media_count++;
130 if (media_id >= MAX_MEDIA) {
131 warn("Attempt to register more than %u media\n", MAX_MEDIA);
135 for (i = 0; i < media_id; i++) {
136 if (media_list[i].type_id == media_type) {
137 warn("Attempt to register second media with type %u\n",
142 if (!strcmp(name, media_list[i].name)) {
143 warn("Attempt to re-register media name <%s>\n", name);
149 m_ptr = &media_list[media_id];
150 m_ptr->type_id = media_type;
151 m_ptr->send_msg = send_msg;
152 m_ptr->enable_bearer = enable;
153 m_ptr->disable_bearer = disable;
154 m_ptr->addr2str = addr2str;
155 memcpy(&m_ptr->bcast_addr, bcast_addr, sizeof(*bcast_addr));
157 strcpy(m_ptr->name, name);
158 m_ptr->priority = bearer_priority;
159 m_ptr->tolerance = link_tolerance;
160 m_ptr->window = send_window_limit;
161 dbg("Media <%s> registered\n", name);
164 write_unlock_bh(&net_lock);
169 * media_addr_printf - record media address in print buffer
172 void media_addr_printf(struct print_buf *pb, struct tipc_media_addr *a)
178 media_type = ntohl(a->type);
179 for (i = 0, m_ptr = media_list; i < media_count; i++, m_ptr++) {
180 if (m_ptr->type_id == media_type)
184 if ((i < media_count) && (m_ptr->addr2str != NULL)) {
185 char addr_str[MAX_ADDR_STR];
187 tipc_printf(pb, "%s(%s) ", m_ptr->name,
188 m_ptr->addr2str(a, addr_str, sizeof(addr_str)));
190 unchar *addr = (unchar *)&a->dev_addr;
192 tipc_printf(pb, "UNKNOWN(%u):", media_type);
193 for (i = 0; i < (sizeof(*a) - sizeof(a->type)); i++) {
194 tipc_printf(pb, "%02x ", addr[i]);
200 * media_get_names - record names of registered media in buffer
203 struct sk_buff *media_get_names(void)
209 buf = cfg_reply_alloc(MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME));
213 read_lock_bh(&net_lock);
214 for (i = 0, m_ptr = media_list; i < media_count; i++, m_ptr++) {
215 cfg_append_tlv(buf, TIPC_TLV_MEDIA_NAME, m_ptr->name,
216 strlen(m_ptr->name) + 1);
218 read_unlock_bh(&net_lock);
223 * bearer_name_validate - validate & (optionally) deconstruct bearer name
224 * @name - ptr to bearer name string
225 * @name_parts - ptr to area for bearer name components (or NULL if not needed)
227 * Returns 1 if bearer name is valid, otherwise 0.
230 static int bearer_name_validate(const char *name,
231 struct bearer_name *name_parts)
233 char name_copy[TIPC_MAX_BEARER_NAME];
239 /* copy bearer name & ensure length is OK */
241 name_copy[TIPC_MAX_BEARER_NAME - 1] = 0;
242 /* need above in case non-Posix strncpy() doesn't pad with nulls */
243 strncpy(name_copy, name, TIPC_MAX_BEARER_NAME);
244 if (name_copy[TIPC_MAX_BEARER_NAME - 1] != 0)
247 /* ensure all component parts of bearer name are present */
249 media_name = name_copy;
250 if ((if_name = strchr(media_name, ':')) == NULL)
253 media_len = if_name - media_name;
254 if_len = strlen(if_name) + 1;
256 /* validate component parts of bearer name */
258 if ((media_len <= 1) || (media_len > TIPC_MAX_MEDIA_NAME) ||
259 (if_len <= 1) || (if_len > TIPC_MAX_IF_NAME) ||
260 (strspn(media_name, tipc_alphabet) != (media_len - 1)) ||
261 (strspn(if_name, tipc_alphabet) != (if_len - 1)))
264 /* return bearer name components, if necessary */
267 strcpy(name_parts->media_name, media_name);
268 strcpy(name_parts->if_name, if_name);
274 * bearer_find - locates bearer object with matching bearer name
277 static struct bearer *bearer_find(const char *name)
279 struct bearer *b_ptr;
282 for (i = 0, b_ptr = bearers; i < MAX_BEARERS; i++, b_ptr++) {
283 if (b_ptr->active && (!strcmp(b_ptr->publ.name, name)))
290 * bearer_find - locates bearer object with matching interface name
293 struct bearer *bearer_find_interface(const char *if_name)
295 struct bearer *b_ptr;
299 for (i = 0, b_ptr = bearers; i < MAX_BEARERS; i++, b_ptr++) {
302 b_if_name = strchr(b_ptr->publ.name, ':') + 1;
303 if (!strcmp(b_if_name, if_name))
310 * bearer_get_names - record names of bearers in buffer
313 struct sk_buff *bearer_get_names(void)
317 struct bearer *b_ptr;
320 buf = cfg_reply_alloc(MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME));
324 read_lock_bh(&net_lock);
325 for (i = 0, m_ptr = media_list; i < media_count; i++, m_ptr++) {
326 for (j = 0; j < MAX_BEARERS; j++) {
328 if (b_ptr->active && (b_ptr->media == m_ptr)) {
329 cfg_append_tlv(buf, TIPC_TLV_BEARER_NAME,
331 strlen(b_ptr->publ.name) + 1);
335 read_unlock_bh(&net_lock);
339 void bearer_add_dest(struct bearer *b_ptr, u32 dest)
341 nmap_add(&b_ptr->nodes, dest);
342 disc_update_link_req(b_ptr->link_req);
346 void bearer_remove_dest(struct bearer *b_ptr, u32 dest)
348 nmap_remove(&b_ptr->nodes, dest);
349 disc_update_link_req(b_ptr->link_req);
354 * bearer_push(): Resolve bearer congestion. Force the waiting
355 * links to push out their unsent packets, one packet per link
356 * per iteration, until all packets are gone or congestion reoccurs.
357 * 'net_lock' is read_locked when this function is called
358 * bearer.lock must be taken before calling
359 * Returns binary true(1) ore false(0)
361 static int bearer_push(struct bearer *b_ptr)
364 struct link *ln, *tln;
366 if (b_ptr->publ.blocked)
369 while (!list_empty(&b_ptr->cong_links) && (res != PUSH_FAILED)) {
370 list_for_each_entry_safe(ln, tln, &b_ptr->cong_links, link_list) {
371 res = link_push_packet(ln);
372 if (res == PUSH_FAILED)
374 if (res == PUSH_FINISHED)
375 list_move_tail(&ln->link_list, &b_ptr->links);
378 return list_empty(&b_ptr->cong_links);
381 void bearer_lock_push(struct bearer *b_ptr)
385 spin_lock_bh(&b_ptr->publ.lock);
386 res = bearer_push(b_ptr);
387 spin_unlock_bh(&b_ptr->publ.lock);
394 * Interrupt enabling new requests after bearer congestion or blocking:
397 void tipc_continue(struct tipc_bearer *tb_ptr)
399 struct bearer *b_ptr = (struct bearer *)tb_ptr;
401 spin_lock_bh(&b_ptr->publ.lock);
402 b_ptr->continue_count++;
403 if (!list_empty(&b_ptr->cong_links))
404 k_signal((Handler)bearer_lock_push, (unsigned long)b_ptr);
405 b_ptr->publ.blocked = 0;
406 spin_unlock_bh(&b_ptr->publ.lock);
410 * Schedule link for sending of messages after the bearer
411 * has been deblocked by 'continue()'. This method is called
412 * when somebody tries to send a message via this link while
413 * the bearer is congested. 'net_lock' is in read_lock here
414 * bearer.lock is busy
417 static void bearer_schedule_unlocked(struct bearer *b_ptr, struct link *l_ptr)
419 list_move_tail(&l_ptr->link_list, &b_ptr->cong_links);
423 * Schedule link for sending of messages after the bearer
424 * has been deblocked by 'continue()'. This method is called
425 * when somebody tries to send a message via this link while
426 * the bearer is congested. 'net_lock' is in read_lock here,
427 * bearer.lock is free
430 void bearer_schedule(struct bearer *b_ptr, struct link *l_ptr)
432 spin_lock_bh(&b_ptr->publ.lock);
433 bearer_schedule_unlocked(b_ptr, l_ptr);
434 spin_unlock_bh(&b_ptr->publ.lock);
439 * bearer_resolve_congestion(): Check if there is bearer congestion,
440 * and if there is, try to resolve it before returning.
441 * 'net_lock' is read_locked when this function is called
443 int bearer_resolve_congestion(struct bearer *b_ptr, struct link *l_ptr)
447 if (list_empty(&b_ptr->cong_links))
449 spin_lock_bh(&b_ptr->publ.lock);
450 if (!bearer_push(b_ptr)) {
451 bearer_schedule_unlocked(b_ptr, l_ptr);
454 spin_unlock_bh(&b_ptr->publ.lock);
460 * tipc_enable_bearer - enable bearer with the given name
463 int tipc_enable_bearer(const char *name, u32 bcast_scope, u32 priority)
465 struct bearer *b_ptr;
467 struct bearer_name b_name;
468 char addr_string[16];
474 if (tipc_mode != TIPC_NET_MODE)
476 if (!bearer_name_validate(name, &b_name) ||
477 !addr_domain_valid(bcast_scope) ||
478 !in_scope(bcast_scope, tipc_own_addr) ||
479 (priority > TIPC_NUM_LINK_PRI))
482 write_lock_bh(&net_lock);
486 m_ptr = media_find(b_name.media_name);
488 warn("No media <%s>\n", b_name.media_name);
491 if (priority == TIPC_NUM_LINK_PRI)
492 priority = m_ptr->priority;
495 bearer_id = MAX_BEARERS;
497 for (i = MAX_BEARERS; i-- != 0; ) {
498 if (!bearers[i].active) {
502 if (!strcmp(name, bearers[i].publ.name)) {
503 warn("Bearer <%s> already enabled\n", name);
506 if ((bearers[i].priority == priority) &&
507 (++with_this_prio > 2)) {
508 if (priority-- == 0) {
509 warn("Third bearer <%s> with priority %u, unable to lower to %u\n",
510 name, priority + 1, priority);
513 warn("Third bearer <%s> with priority %u, lowering to %u\n",
514 name, priority + 1, priority);
518 if (bearer_id >= MAX_BEARERS) {
519 warn("Attempt to enable more than %d bearers\n", MAX_BEARERS);
523 b_ptr = &bearers[bearer_id];
524 memset(b_ptr, 0, sizeof(struct bearer));
526 strcpy(b_ptr->publ.name, name);
527 res = m_ptr->enable_bearer(&b_ptr->publ);
529 warn("Failed to enable bearer <%s>\n", name);
533 b_ptr->identity = bearer_id;
534 b_ptr->media = m_ptr;
535 b_ptr->net_plane = bearer_id + 'A';
537 b_ptr->detect_scope = bcast_scope;
538 b_ptr->priority = priority;
539 INIT_LIST_HEAD(&b_ptr->cong_links);
540 INIT_LIST_HEAD(&b_ptr->links);
542 b_ptr->link_req = disc_init_link_req(b_ptr, &m_ptr->bcast_addr,
545 b_ptr->publ.lock = SPIN_LOCK_UNLOCKED;
546 write_unlock_bh(&net_lock);
547 info("Enabled bearer <%s>, discovery domain %s\n",
548 name, addr_string_fill(addr_string, bcast_scope));
551 write_unlock_bh(&net_lock);
556 * tipc_block_bearer(): Block the bearer with the given name,
557 * and reset all its links
560 int tipc_block_bearer(const char *name)
562 struct bearer *b_ptr = 0;
564 struct link *temp_l_ptr;
566 if (tipc_mode != TIPC_NET_MODE)
569 read_lock_bh(&net_lock);
570 b_ptr = bearer_find(name);
572 warn("Attempt to block unknown bearer <%s>\n", name);
573 read_unlock_bh(&net_lock);
577 spin_lock_bh(&b_ptr->publ.lock);
578 b_ptr->publ.blocked = 1;
579 list_for_each_entry_safe(l_ptr, temp_l_ptr, &b_ptr->links, link_list) {
580 struct node *n_ptr = l_ptr->owner;
582 spin_lock_bh(&n_ptr->lock);
584 spin_unlock_bh(&n_ptr->lock);
586 spin_unlock_bh(&b_ptr->publ.lock);
587 read_unlock_bh(&net_lock);
588 info("Blocked bearer <%s>\n", name);
595 * Note: This routine assumes caller holds net_lock.
598 static int bearer_disable(const char *name)
600 struct bearer *b_ptr;
602 struct link *temp_l_ptr;
604 if (tipc_mode != TIPC_NET_MODE)
607 b_ptr = bearer_find(name);
609 warn("Attempt to disable unknown bearer <%s>\n", name);
613 disc_stop_link_req(b_ptr->link_req);
614 spin_lock_bh(&b_ptr->publ.lock);
615 b_ptr->link_req = NULL;
616 b_ptr->publ.blocked = 1;
617 if (b_ptr->media->disable_bearer) {
618 spin_unlock_bh(&b_ptr->publ.lock);
619 write_unlock_bh(&net_lock);
620 b_ptr->media->disable_bearer(&b_ptr->publ);
621 write_lock_bh(&net_lock);
622 spin_lock_bh(&b_ptr->publ.lock);
624 list_for_each_entry_safe(l_ptr, temp_l_ptr, &b_ptr->links, link_list) {
627 spin_unlock_bh(&b_ptr->publ.lock);
628 info("Disabled bearer <%s>\n", name);
629 memset(b_ptr, 0, sizeof(struct bearer));
633 int tipc_disable_bearer(const char *name)
637 write_lock_bh(&net_lock);
638 res = bearer_disable(name);
639 write_unlock_bh(&net_lock);
645 int bearer_init(void)
649 write_lock_bh(&net_lock);
650 bearers = kmalloc(MAX_BEARERS * sizeof(struct bearer), GFP_ATOMIC);
651 media_list = kmalloc(MAX_MEDIA * sizeof(struct media), GFP_ATOMIC);
652 if (bearers && media_list) {
653 memset(bearers, 0, MAX_BEARERS * sizeof(struct bearer));
654 memset(media_list, 0, MAX_MEDIA * sizeof(struct media));
663 write_unlock_bh(&net_lock);
667 void bearer_stop(void)
674 for (i = 0; i < MAX_BEARERS; i++) {
675 if (bearers[i].active)
676 bearers[i].publ.blocked = 1;
678 for (i = 0; i < MAX_BEARERS; i++) {
679 if (bearers[i].active)
680 bearer_disable(bearers[i].publ.name);