blob: 885da94be4ac2361c1eaa60493164eb882928c03 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/bearer.c: TIPC bearer code
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Per Liden593a5f22006-01-11 19:14:19 +01004 * Copyright (c) 1996-2006, Ericsson AB
Allan Stephense91ed0b2006-10-16 21:44:59 -07005 * Copyright (c) 2004-2006, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 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.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * 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
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
38#include "config.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "bearer.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010040#include "port.h"
41#include "discover.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010042
43#define MAX_ADDR_STR 32
44
Neil Hormand0021b22010-03-03 08:31:23 +000045static struct media media_list[MAX_MEDIA];
Per Lidenb97bf3f2006-01-02 19:04:38 +010046static u32 media_count = 0;
47
Neil Hormand0021b22010-03-03 08:31:23 +000048struct bearer tipc_bearers[MAX_BEARERS];
Per Lidenb97bf3f2006-01-02 19:04:38 +010049
50/**
51 * media_name_valid - validate media name
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090052 *
Per Lidenb97bf3f2006-01-02 19:04:38 +010053 * Returns 1 if media name is valid, otherwise 0.
54 */
55
56static int media_name_valid(const char *name)
57{
58 u32 len;
59
60 len = strlen(name);
61 if ((len + 1) > TIPC_MAX_MEDIA_NAME)
62 return 0;
Eric Dumazeta02cec22010-09-22 20:43:57 +000063 return strspn(name, tipc_alphabet) == len;
Per Lidenb97bf3f2006-01-02 19:04:38 +010064}
65
66/**
67 * media_find - locates specified media object by name
68 */
69
70static struct media *media_find(const char *name)
71{
72 struct media *m_ptr;
73 u32 i;
74
75 for (i = 0, m_ptr = media_list; i < media_count; i++, m_ptr++) {
76 if (!strcmp(m_ptr->name, name))
77 return m_ptr;
78 }
Sam Ravnborg1fc54d82006-03-20 22:36:47 -080079 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +010080}
81
82/**
83 * tipc_register_media - register a media type
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090084 *
Per Lidenb97bf3f2006-01-02 19:04:38 +010085 * Bearers for this media type must be activated separately at a later stage.
86 */
87
88int tipc_register_media(u32 media_type,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090089 char *name,
90 int (*enable)(struct tipc_bearer *),
91 void (*disable)(struct tipc_bearer *),
92 int (*send_msg)(struct sk_buff *,
Per Lidenb97bf3f2006-01-02 19:04:38 +010093 struct tipc_bearer *,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090094 struct tipc_media_addr *),
Per Lidenb97bf3f2006-01-02 19:04:38 +010095 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)
101{
102 struct media *m_ptr;
103 u32 media_id;
104 u32 i;
105 int res = -EINVAL;
106
Per Liden4323add2006-01-18 00:38:21 +0100107 write_lock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100108
Neil Hormand0021b22010-03-03 08:31:23 +0000109 if (tipc_mode != TIPC_NET_MODE) {
110 warn("Media <%s> rejected, not in networked mode yet\n", name);
111 goto exit;
112 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100113 if (!media_name_valid(name)) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700114 warn("Media <%s> rejected, illegal name\n", name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100115 goto exit;
116 }
117 if (!bcast_addr) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700118 warn("Media <%s> rejected, no broadcast address\n", name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100119 goto exit;
120 }
roel kluinb3df9a52009-08-27 02:03:15 +0000121 if ((bearer_priority < TIPC_MIN_LINK_PRI) ||
Per Liden16cb4b32006-01-13 22:22:22 +0100122 (bearer_priority > TIPC_MAX_LINK_PRI)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900123 warn("Media <%s> rejected, illegal priority (%u)\n", name,
Allan Stephensa10bd922006-06-25 23:52:17 -0700124 bearer_priority);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125 goto exit;
126 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900127 if ((link_tolerance < TIPC_MIN_LINK_TOL) ||
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128 (link_tolerance > TIPC_MAX_LINK_TOL)) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700129 warn("Media <%s> rejected, illegal tolerance (%u)\n", name,
130 link_tolerance);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100131 goto exit;
132 }
133
134 media_id = media_count++;
135 if (media_id >= MAX_MEDIA) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700136 warn("Media <%s> rejected, media limit reached (%u)\n", name,
137 MAX_MEDIA);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100138 media_count--;
139 goto exit;
140 }
141 for (i = 0; i < media_id; i++) {
142 if (media_list[i].type_id == media_type) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700143 warn("Media <%s> rejected, duplicate type (%u)\n", name,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100144 media_type);
145 media_count--;
146 goto exit;
147 }
148 if (!strcmp(name, media_list[i].name)) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700149 warn("Media <%s> rejected, duplicate name\n", name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100150 media_count--;
151 goto exit;
152 }
153 }
154
155 m_ptr = &media_list[media_id];
156 m_ptr->type_id = media_type;
157 m_ptr->send_msg = send_msg;
158 m_ptr->enable_bearer = enable;
159 m_ptr->disable_bearer = disable;
160 m_ptr->addr2str = addr2str;
161 memcpy(&m_ptr->bcast_addr, bcast_addr, sizeof(*bcast_addr));
162 m_ptr->bcast = 1;
163 strcpy(m_ptr->name, name);
164 m_ptr->priority = bearer_priority;
165 m_ptr->tolerance = link_tolerance;
166 m_ptr->window = send_window_limit;
167 dbg("Media <%s> registered\n", name);
168 res = 0;
169exit:
Per Liden4323add2006-01-18 00:38:21 +0100170 write_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171 return res;
172}
173
174/**
Per Liden4323add2006-01-18 00:38:21 +0100175 * tipc_media_addr_printf - record media address in print buffer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100176 */
177
Per Liden4323add2006-01-18 00:38:21 +0100178void tipc_media_addr_printf(struct print_buf *pb, struct tipc_media_addr *a)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100179{
180 struct media *m_ptr;
181 u32 media_type;
182 u32 i;
183
184 media_type = ntohl(a->type);
185 for (i = 0, m_ptr = media_list; i < media_count; i++, m_ptr++) {
186 if (m_ptr->type_id == media_type)
187 break;
188 }
189
190 if ((i < media_count) && (m_ptr->addr2str != NULL)) {
191 char addr_str[MAX_ADDR_STR];
192
Allan Stephense91ed0b2006-10-16 21:44:59 -0700193 tipc_printf(pb, "%s(%s)", m_ptr->name,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100194 m_ptr->addr2str(a, addr_str, sizeof(addr_str)));
195 } else {
196 unchar *addr = (unchar *)&a->dev_addr;
197
Allan Stephense91ed0b2006-10-16 21:44:59 -0700198 tipc_printf(pb, "UNKNOWN(%u)", media_type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100199 for (i = 0; i < (sizeof(*a) - sizeof(a->type)); i++) {
Allan Stephense91ed0b2006-10-16 21:44:59 -0700200 tipc_printf(pb, "-%02x", addr[i]);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100201 }
202 }
203}
204
205/**
Per Liden4323add2006-01-18 00:38:21 +0100206 * tipc_media_get_names - record names of registered media in buffer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100207 */
208
Per Liden4323add2006-01-18 00:38:21 +0100209struct sk_buff *tipc_media_get_names(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100210{
211 struct sk_buff *buf;
212 struct media *m_ptr;
213 int i;
214
Per Liden4323add2006-01-18 00:38:21 +0100215 buf = tipc_cfg_reply_alloc(MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216 if (!buf)
217 return NULL;
218
Per Liden4323add2006-01-18 00:38:21 +0100219 read_lock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220 for (i = 0, m_ptr = media_list; i < media_count; i++, m_ptr++) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900221 tipc_cfg_append_tlv(buf, TIPC_TLV_MEDIA_NAME, m_ptr->name,
Per Liden4323add2006-01-18 00:38:21 +0100222 strlen(m_ptr->name) + 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100223 }
Per Liden4323add2006-01-18 00:38:21 +0100224 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100225 return buf;
226}
227
228/**
229 * bearer_name_validate - validate & (optionally) deconstruct bearer name
230 * @name - ptr to bearer name string
231 * @name_parts - ptr to area for bearer name components (or NULL if not needed)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900232 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100233 * Returns 1 if bearer name is valid, otherwise 0.
234 */
235
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900236static int bearer_name_validate(const char *name,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100237 struct bearer_name *name_parts)
238{
239 char name_copy[TIPC_MAX_BEARER_NAME];
240 char *media_name;
241 char *if_name;
242 u32 media_len;
243 u32 if_len;
244
245 /* copy bearer name & ensure length is OK */
246
247 name_copy[TIPC_MAX_BEARER_NAME - 1] = 0;
248 /* need above in case non-Posix strncpy() doesn't pad with nulls */
249 strncpy(name_copy, name, TIPC_MAX_BEARER_NAME);
250 if (name_copy[TIPC_MAX_BEARER_NAME - 1] != 0)
251 return 0;
252
253 /* ensure all component parts of bearer name are present */
254
255 media_name = name_copy;
256 if ((if_name = strchr(media_name, ':')) == NULL)
257 return 0;
258 *(if_name++) = 0;
259 media_len = if_name - media_name;
260 if_len = strlen(if_name) + 1;
261
262 /* validate component parts of bearer name */
263
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900264 if ((media_len <= 1) || (media_len > TIPC_MAX_MEDIA_NAME) ||
265 (if_len <= 1) || (if_len > TIPC_MAX_IF_NAME) ||
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266 (strspn(media_name, tipc_alphabet) != (media_len - 1)) ||
267 (strspn(if_name, tipc_alphabet) != (if_len - 1)))
268 return 0;
269
270 /* return bearer name components, if necessary */
271
272 if (name_parts) {
273 strcpy(name_parts->media_name, media_name);
274 strcpy(name_parts->if_name, if_name);
275 }
276 return 1;
277}
278
279/**
280 * bearer_find - locates bearer object with matching bearer name
281 */
282
283static struct bearer *bearer_find(const char *name)
284{
285 struct bearer *b_ptr;
286 u32 i;
287
Per Liden4323add2006-01-18 00:38:21 +0100288 for (i = 0, b_ptr = tipc_bearers; i < MAX_BEARERS; i++, b_ptr++) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100289 if (b_ptr->active && (!strcmp(b_ptr->publ.name, name)))
290 return b_ptr;
291 }
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800292 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293}
294
295/**
Per Liden4323add2006-01-18 00:38:21 +0100296 * tipc_bearer_find_interface - locates bearer object with matching interface name
Per Lidenb97bf3f2006-01-02 19:04:38 +0100297 */
298
Per Liden4323add2006-01-18 00:38:21 +0100299struct bearer *tipc_bearer_find_interface(const char *if_name)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300{
301 struct bearer *b_ptr;
302 char *b_if_name;
303 u32 i;
304
Per Liden4323add2006-01-18 00:38:21 +0100305 for (i = 0, b_ptr = tipc_bearers; i < MAX_BEARERS; i++, b_ptr++) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100306 if (!b_ptr->active)
307 continue;
308 b_if_name = strchr(b_ptr->publ.name, ':') + 1;
309 if (!strcmp(b_if_name, if_name))
310 return b_ptr;
311 }
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800312 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100313}
314
315/**
Per Liden4323add2006-01-18 00:38:21 +0100316 * tipc_bearer_get_names - record names of bearers in buffer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100317 */
318
Per Liden4323add2006-01-18 00:38:21 +0100319struct sk_buff *tipc_bearer_get_names(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320{
321 struct sk_buff *buf;
322 struct media *m_ptr;
323 struct bearer *b_ptr;
324 int i, j;
325
Per Liden4323add2006-01-18 00:38:21 +0100326 buf = tipc_cfg_reply_alloc(MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100327 if (!buf)
328 return NULL;
329
Per Liden4323add2006-01-18 00:38:21 +0100330 read_lock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100331 for (i = 0, m_ptr = media_list; i < media_count; i++, m_ptr++) {
332 for (j = 0; j < MAX_BEARERS; j++) {
Per Liden4323add2006-01-18 00:38:21 +0100333 b_ptr = &tipc_bearers[j];
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334 if (b_ptr->active && (b_ptr->media == m_ptr)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900335 tipc_cfg_append_tlv(buf, TIPC_TLV_BEARER_NAME,
336 b_ptr->publ.name,
Per Liden4323add2006-01-18 00:38:21 +0100337 strlen(b_ptr->publ.name) + 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100338 }
339 }
340 }
Per Liden4323add2006-01-18 00:38:21 +0100341 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100342 return buf;
343}
344
Per Liden4323add2006-01-18 00:38:21 +0100345void tipc_bearer_add_dest(struct bearer *b_ptr, u32 dest)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100346{
Per Liden4323add2006-01-18 00:38:21 +0100347 tipc_nmap_add(&b_ptr->nodes, dest);
348 tipc_disc_update_link_req(b_ptr->link_req);
349 tipc_bcbearer_sort();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100350}
351
Per Liden4323add2006-01-18 00:38:21 +0100352void tipc_bearer_remove_dest(struct bearer *b_ptr, u32 dest)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100353{
Per Liden4323add2006-01-18 00:38:21 +0100354 tipc_nmap_remove(&b_ptr->nodes, dest);
355 tipc_disc_update_link_req(b_ptr->link_req);
356 tipc_bcbearer_sort();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100357}
358
359/*
360 * bearer_push(): Resolve bearer congestion. Force the waiting
361 * links to push out their unsent packets, one packet per link
362 * per iteration, until all packets are gone or congestion reoccurs.
Per Liden4323add2006-01-18 00:38:21 +0100363 * 'tipc_net_lock' is read_locked when this function is called
Per Lidenb97bf3f2006-01-02 19:04:38 +0100364 * bearer.lock must be taken before calling
365 * Returns binary true(1) ore false(0)
366 */
367static int bearer_push(struct bearer *b_ptr)
368{
Allan Stephens0e35fd52008-07-14 22:44:01 -0700369 u32 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100370 struct link *ln, *tln;
371
372 if (b_ptr->publ.blocked)
373 return 0;
374
375 while (!list_empty(&b_ptr->cong_links) && (res != PUSH_FAILED)) {
376 list_for_each_entry_safe(ln, tln, &b_ptr->cong_links, link_list) {
Per Liden4323add2006-01-18 00:38:21 +0100377 res = tipc_link_push_packet(ln);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100378 if (res == PUSH_FAILED)
379 break;
380 if (res == PUSH_FINISHED)
381 list_move_tail(&ln->link_list, &b_ptr->links);
382 }
383 }
384 return list_empty(&b_ptr->cong_links);
385}
386
Per Liden4323add2006-01-18 00:38:21 +0100387void tipc_bearer_lock_push(struct bearer *b_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388{
389 int res;
390
391 spin_lock_bh(&b_ptr->publ.lock);
392 res = bearer_push(b_ptr);
393 spin_unlock_bh(&b_ptr->publ.lock);
394 if (res)
Per Liden4323add2006-01-18 00:38:21 +0100395 tipc_bcbearer_push();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100396}
397
398
399/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900400 * Interrupt enabling new requests after bearer congestion or blocking:
401 * See bearer_send().
Per Lidenb97bf3f2006-01-02 19:04:38 +0100402 */
403void tipc_continue(struct tipc_bearer *tb_ptr)
404{
405 struct bearer *b_ptr = (struct bearer *)tb_ptr;
406
407 spin_lock_bh(&b_ptr->publ.lock);
408 b_ptr->continue_count++;
409 if (!list_empty(&b_ptr->cong_links))
Per Liden4323add2006-01-18 00:38:21 +0100410 tipc_k_signal((Handler)tipc_bearer_lock_push, (unsigned long)b_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100411 b_ptr->publ.blocked = 0;
412 spin_unlock_bh(&b_ptr->publ.lock);
413}
414
415/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900416 * Schedule link for sending of messages after the bearer
417 * has been deblocked by 'continue()'. This method is called
418 * when somebody tries to send a message via this link while
Per Liden4323add2006-01-18 00:38:21 +0100419 * the bearer is congested. 'tipc_net_lock' is in read_lock here
Per Lidenb97bf3f2006-01-02 19:04:38 +0100420 * bearer.lock is busy
421 */
422
Per Liden4323add2006-01-18 00:38:21 +0100423static void tipc_bearer_schedule_unlocked(struct bearer *b_ptr, struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100424{
425 list_move_tail(&l_ptr->link_list, &b_ptr->cong_links);
426}
427
428/*
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900429 * Schedule link for sending of messages after the bearer
430 * has been deblocked by 'continue()'. This method is called
431 * when somebody tries to send a message via this link while
Per Liden4323add2006-01-18 00:38:21 +0100432 * the bearer is congested. 'tipc_net_lock' is in read_lock here,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100433 * bearer.lock is free
434 */
435
Per Liden4323add2006-01-18 00:38:21 +0100436void tipc_bearer_schedule(struct bearer *b_ptr, struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437{
438 spin_lock_bh(&b_ptr->publ.lock);
Per Liden4323add2006-01-18 00:38:21 +0100439 tipc_bearer_schedule_unlocked(b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100440 spin_unlock_bh(&b_ptr->publ.lock);
441}
442
443
444/*
Per Liden4323add2006-01-18 00:38:21 +0100445 * tipc_bearer_resolve_congestion(): Check if there is bearer congestion,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100446 * and if there is, try to resolve it before returning.
Per Liden4323add2006-01-18 00:38:21 +0100447 * 'tipc_net_lock' is read_locked when this function is called
Per Lidenb97bf3f2006-01-02 19:04:38 +0100448 */
Per Liden4323add2006-01-18 00:38:21 +0100449int tipc_bearer_resolve_congestion(struct bearer *b_ptr, struct link *l_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450{
451 int res = 1;
452
453 if (list_empty(&b_ptr->cong_links))
454 return 1;
455 spin_lock_bh(&b_ptr->publ.lock);
456 if (!bearer_push(b_ptr)) {
Per Liden4323add2006-01-18 00:38:21 +0100457 tipc_bearer_schedule_unlocked(b_ptr, l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100458 res = 0;
459 }
460 spin_unlock_bh(&b_ptr->publ.lock);
461 return res;
462}
463
Allan Stephensb274f4a2010-05-11 14:30:16 +0000464/**
465 * tipc_bearer_congested - determines if bearer is currently congested
466 */
467
468int tipc_bearer_congested(struct bearer *b_ptr, struct link *l_ptr)
469{
470 if (unlikely(b_ptr->publ.blocked))
471 return 1;
472 if (likely(list_empty(&b_ptr->cong_links)))
473 return 0;
474 return !tipc_bearer_resolve_congestion(b_ptr, l_ptr);
475}
Per Lidenb97bf3f2006-01-02 19:04:38 +0100476
477/**
478 * tipc_enable_bearer - enable bearer with the given name
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900479 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100480
481int tipc_enable_bearer(const char *name, u32 bcast_scope, u32 priority)
482{
483 struct bearer *b_ptr;
484 struct media *m_ptr;
485 struct bearer_name b_name;
486 char addr_string[16];
487 u32 bearer_id;
488 u32 with_this_prio;
489 u32 i;
490 int res = -EINVAL;
491
Allan Stephensa10bd922006-06-25 23:52:17 -0700492 if (tipc_mode != TIPC_NET_MODE) {
493 warn("Bearer <%s> rejected, not supported in standalone mode\n",
494 name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100495 return -ENOPROTOOPT;
Allan Stephensa10bd922006-06-25 23:52:17 -0700496 }
497 if (!bearer_name_validate(name, &b_name)) {
498 warn("Bearer <%s> rejected, illegal name\n", name);
Per Liden16cb4b32006-01-13 22:22:22 +0100499 return -EINVAL;
Allan Stephensa10bd922006-06-25 23:52:17 -0700500 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900501 if (!tipc_addr_domain_valid(bcast_scope) ||
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000502 !tipc_in_scope(bcast_scope, tipc_own_addr)) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700503 warn("Bearer <%s> rejected, illegal broadcast scope\n", name);
504 return -EINVAL;
505 }
Per Liden16cb4b32006-01-13 22:22:22 +0100506 if ((priority < TIPC_MIN_LINK_PRI ||
507 priority > TIPC_MAX_LINK_PRI) &&
Allan Stephensa10bd922006-06-25 23:52:17 -0700508 (priority != TIPC_MEDIA_LINK_PRI)) {
509 warn("Bearer <%s> rejected, illegal priority\n", name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100510 return -EINVAL;
Allan Stephensa10bd922006-06-25 23:52:17 -0700511 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100512
Per Liden4323add2006-01-18 00:38:21 +0100513 write_lock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100514
515 m_ptr = media_find(b_name.media_name);
516 if (!m_ptr) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700517 warn("Bearer <%s> rejected, media <%s> not registered\n", name,
518 b_name.media_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100519 goto failed;
520 }
Per Liden16cb4b32006-01-13 22:22:22 +0100521
522 if (priority == TIPC_MEDIA_LINK_PRI)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100523 priority = m_ptr->priority;
524
525restart:
526 bearer_id = MAX_BEARERS;
527 with_this_prio = 1;
528 for (i = MAX_BEARERS; i-- != 0; ) {
Per Liden4323add2006-01-18 00:38:21 +0100529 if (!tipc_bearers[i].active) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100530 bearer_id = i;
531 continue;
532 }
Per Liden4323add2006-01-18 00:38:21 +0100533 if (!strcmp(name, tipc_bearers[i].publ.name)) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700534 warn("Bearer <%s> rejected, already enabled\n", name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535 goto failed;
536 }
Per Liden4323add2006-01-18 00:38:21 +0100537 if ((tipc_bearers[i].priority == priority) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538 (++with_this_prio > 2)) {
539 if (priority-- == 0) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700540 warn("Bearer <%s> rejected, duplicate priority\n",
541 name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100542 goto failed;
543 }
Allan Stephensa10bd922006-06-25 23:52:17 -0700544 warn("Bearer <%s> priority adjustment required %u->%u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545 name, priority + 1, priority);
546 goto restart;
547 }
548 }
549 if (bearer_id >= MAX_BEARERS) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900550 warn("Bearer <%s> rejected, bearer limit reached (%u)\n",
Allan Stephensa10bd922006-06-25 23:52:17 -0700551 name, MAX_BEARERS);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100552 goto failed;
553 }
554
Per Liden4323add2006-01-18 00:38:21 +0100555 b_ptr = &tipc_bearers[bearer_id];
Per Lidenb97bf3f2006-01-02 19:04:38 +0100556 strcpy(b_ptr->publ.name, name);
557 res = m_ptr->enable_bearer(&b_ptr->publ);
558 if (res) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700559 warn("Bearer <%s> rejected, enable failure (%d)\n", name, -res);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100560 goto failed;
561 }
562
563 b_ptr->identity = bearer_id;
564 b_ptr->media = m_ptr;
565 b_ptr->net_plane = bearer_id + 'A';
566 b_ptr->active = 1;
567 b_ptr->detect_scope = bcast_scope;
568 b_ptr->priority = priority;
569 INIT_LIST_HEAD(&b_ptr->cong_links);
570 INIT_LIST_HEAD(&b_ptr->links);
571 if (m_ptr->bcast) {
Per Liden4323add2006-01-18 00:38:21 +0100572 b_ptr->link_req = tipc_disc_init_link_req(b_ptr, &m_ptr->bcast_addr,
573 bcast_scope, 2);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100574 }
Ingo Molnar34af9462006-06-27 02:53:55 -0700575 spin_lock_init(&b_ptr->publ.lock);
Per Liden4323add2006-01-18 00:38:21 +0100576 write_unlock_bh(&tipc_net_lock);
Per Liden16cb4b32006-01-13 22:22:22 +0100577 info("Enabled bearer <%s>, discovery domain %s, priority %u\n",
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000578 name, tipc_addr_string_fill(addr_string, bcast_scope), priority);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100579 return 0;
580failed:
Per Liden4323add2006-01-18 00:38:21 +0100581 write_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 return res;
583}
584
585/**
586 * tipc_block_bearer(): Block the bearer with the given name,
587 * and reset all its links
588 */
589
590int tipc_block_bearer(const char *name)
591{
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800592 struct bearer *b_ptr = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100593 struct link *l_ptr;
594 struct link *temp_l_ptr;
595
Per Liden4323add2006-01-18 00:38:21 +0100596 read_lock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100597 b_ptr = bearer_find(name);
598 if (!b_ptr) {
599 warn("Attempt to block unknown bearer <%s>\n", name);
Per Liden4323add2006-01-18 00:38:21 +0100600 read_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100601 return -EINVAL;
602 }
603
Allan Stephensa10bd922006-06-25 23:52:17 -0700604 info("Blocking bearer <%s>\n", name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605 spin_lock_bh(&b_ptr->publ.lock);
606 b_ptr->publ.blocked = 1;
607 list_for_each_entry_safe(l_ptr, temp_l_ptr, &b_ptr->links, link_list) {
David S. Miller6c000552008-09-02 23:38:32 -0700608 struct tipc_node *n_ptr = l_ptr->owner;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609
610 spin_lock_bh(&n_ptr->lock);
Per Liden4323add2006-01-18 00:38:21 +0100611 tipc_link_reset(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100612 spin_unlock_bh(&n_ptr->lock);
613 }
614 spin_unlock_bh(&b_ptr->publ.lock);
Per Liden4323add2006-01-18 00:38:21 +0100615 read_unlock_bh(&tipc_net_lock);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700616 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617}
618
619/**
620 * bearer_disable -
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900621 *
Per Liden4323add2006-01-18 00:38:21 +0100622 * Note: This routine assumes caller holds tipc_net_lock.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100623 */
624
Allan Stephens28cc9372010-11-30 12:00:56 +0000625static void bearer_disable(struct bearer *b_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100626{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100627 struct link *l_ptr;
628 struct link *temp_l_ptr;
629
Allan Stephensccc901e2010-10-14 13:58:26 +0000630 info("Disabling bearer <%s>\n", b_ptr->publ.name);
Per Liden4323add2006-01-18 00:38:21 +0100631 tipc_disc_stop_link_req(b_ptr->link_req);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100632 spin_lock_bh(&b_ptr->publ.lock);
633 b_ptr->link_req = NULL;
634 b_ptr->publ.blocked = 1;
Allan Stephensccc901e2010-10-14 13:58:26 +0000635 b_ptr->media->disable_bearer(&b_ptr->publ);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100636 list_for_each_entry_safe(l_ptr, temp_l_ptr, &b_ptr->links, link_list) {
Per Liden4323add2006-01-18 00:38:21 +0100637 tipc_link_delete(l_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638 }
639 spin_unlock_bh(&b_ptr->publ.lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100640 memset(b_ptr, 0, sizeof(struct bearer));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641}
642
643int tipc_disable_bearer(const char *name)
644{
Allan Stephensccc901e2010-10-14 13:58:26 +0000645 struct bearer *b_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646 int res;
647
Per Liden4323add2006-01-18 00:38:21 +0100648 write_lock_bh(&tipc_net_lock);
Allan Stephensccc901e2010-10-14 13:58:26 +0000649 b_ptr = bearer_find(name);
650 if (b_ptr == NULL) {
651 warn("Attempt to disable unknown bearer <%s>\n", name);
652 res = -EINVAL;
Allan Stephens28cc9372010-11-30 12:00:56 +0000653 } else {
654 bearer_disable(b_ptr);
655 res = 0;
656 }
Per Liden4323add2006-01-18 00:38:21 +0100657 write_unlock_bh(&tipc_net_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100658 return res;
659}
660
661
662
Per Liden4323add2006-01-18 00:38:21 +0100663void tipc_bearer_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100664{
665 u32 i;
666
Per Lidenb97bf3f2006-01-02 19:04:38 +0100667 for (i = 0; i < MAX_BEARERS; i++) {
Per Liden4323add2006-01-18 00:38:21 +0100668 if (tipc_bearers[i].active)
Allan Stephensccc901e2010-10-14 13:58:26 +0000669 bearer_disable(&tipc_bearers[i]);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100670 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100671 media_count = 0;
672}