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