Commit | Line | Data |
---|---|---|
b97bf3fd PL |
1 | /* |
2 | * net/tipc/bearer.c: TIPC bearer code | |
c4307285 | 3 | * |
593a5f22 | 4 | * Copyright (c) 1996-2006, Ericsson AB |
2d627b92 | 5 | * Copyright (c) 2004-2006, 2010-2011, Wind River Systems |
b97bf3fd PL |
6 | * All rights reserved. |
7 | * | |
9ea1fd3c | 8 | * Redistribution and use in source and binary forms, with or without |
b97bf3fd PL |
9 | * modification, are permitted provided that the following conditions are met: |
10 | * | |
9ea1fd3c PL |
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. | |
b97bf3fd | 19 | * |
9ea1fd3c PL |
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 | |
b97bf3fd PL |
34 | * POSSIBILITY OF SUCH DAMAGE. |
35 | */ | |
36 | ||
37 | #include "core.h" | |
38 | #include "config.h" | |
b97bf3fd | 39 | #include "bearer.h" |
b97bf3fd | 40 | #include "discover.h" |
b97bf3fd PL |
41 | |
42 | #define MAX_ADDR_STR 32 | |
43 | ||
358a0d1c | 44 | static struct tipc_media *media_list[MAX_MEDIA]; |
e3ec9c7d | 45 | static u32 media_count; |
b97bf3fd | 46 | |
2d627b92 | 47 | struct tipc_bearer tipc_bearers[MAX_BEARERS]; |
b97bf3fd | 48 | |
3a777ff8 AS |
49 | static void bearer_disable(struct tipc_bearer *b_ptr); |
50 | ||
b97bf3fd PL |
51 | /** |
52 | * media_name_valid - validate media name | |
c4307285 | 53 | * |
b97bf3fd PL |
54 | * Returns 1 if media name is valid, otherwise 0. |
55 | */ | |
b97bf3fd PL |
56 | static 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; | |
a02cec21 | 63 | return strspn(name, tipc_alphabet) == len; |
b97bf3fd PL |
64 | } |
65 | ||
66 | /** | |
5c216e1d | 67 | * tipc_media_find - locates specified media object by name |
b97bf3fd | 68 | */ |
358a0d1c | 69 | struct tipc_media *tipc_media_find(const char *name) |
b97bf3fd | 70 | { |
b97bf3fd PL |
71 | u32 i; |
72 | ||
a31abe8d AS |
73 | for (i = 0; i < media_count; i++) { |
74 | if (!strcmp(media_list[i]->name, name)) | |
75 | return media_list[i]; | |
b97bf3fd | 76 | } |
1fc54d8f | 77 | return NULL; |
b97bf3fd PL |
78 | } |
79 | ||
c79be454 AS |
80 | /** |
81 | * media_find_id - locates specified media object by type identifier | |
82 | */ | |
358a0d1c | 83 | static struct tipc_media *media_find_id(u8 type) |
c79be454 AS |
84 | { |
85 | u32 i; | |
86 | ||
87 | for (i = 0; i < media_count; i++) { | |
a31abe8d AS |
88 | if (media_list[i]->type_id == type) |
89 | return media_list[i]; | |
c79be454 AS |
90 | } |
91 | return NULL; | |
92 | } | |
93 | ||
b97bf3fd PL |
94 | /** |
95 | * tipc_register_media - register a media type | |
c4307285 | 96 | * |
b97bf3fd PL |
97 | * Bearers for this media type must be activated separately at a later stage. |
98 | */ | |
358a0d1c | 99 | int tipc_register_media(struct tipc_media *m_ptr) |
b97bf3fd | 100 | { |
b97bf3fd PL |
101 | int res = -EINVAL; |
102 | ||
4323add6 | 103 | write_lock_bh(&tipc_net_lock); |
b97bf3fd | 104 | |
6c349210 | 105 | if (!media_name_valid(m_ptr->name)) |
d0021b25 | 106 | goto exit; |
3d749a6a AS |
107 | if ((m_ptr->bcast_addr.media_id != m_ptr->type_id) || |
108 | !m_ptr->bcast_addr.broadcast) | |
b97bf3fd | 109 | goto exit; |
6c349210 | 110 | if (m_ptr->priority > TIPC_MAX_LINK_PRI) |
b97bf3fd | 111 | goto exit; |
706767da | 112 | if ((m_ptr->tolerance < TIPC_MIN_LINK_TOL) || |
6c349210 | 113 | (m_ptr->tolerance > TIPC_MAX_LINK_TOL)) |
b97bf3fd | 114 | goto exit; |
6c349210 | 115 | if (media_count >= MAX_MEDIA) |
b97bf3fd | 116 | goto exit; |
5c216e1d | 117 | if (tipc_media_find(m_ptr->name) || media_find_id(m_ptr->type_id)) |
c79be454 | 118 | goto exit; |
b97bf3fd | 119 | |
a31abe8d | 120 | media_list[media_count] = m_ptr; |
c79be454 | 121 | media_count++; |
b97bf3fd PL |
122 | res = 0; |
123 | exit: | |
4323add6 | 124 | write_unlock_bh(&tipc_net_lock); |
6c349210 | 125 | if (res) |
2cf8aa19 | 126 | pr_warn("Media <%s> registration error\n", m_ptr->name); |
b97bf3fd PL |
127 | return res; |
128 | } | |
129 | ||
130 | /** | |
4323add6 | 131 | * tipc_media_addr_printf - record media address in print buffer |
b97bf3fd | 132 | */ |
dc1aed37 | 133 | void tipc_media_addr_printf(char *buf, int len, struct tipc_media_addr *a) |
b97bf3fd | 134 | { |
c61b666e | 135 | char addr_str[MAX_ADDR_STR]; |
358a0d1c | 136 | struct tipc_media *m_ptr; |
dc1aed37 | 137 | int ret; |
b97bf3fd | 138 | |
3d749a6a | 139 | m_ptr = media_find_id(a->media_id); |
b97bf3fd | 140 | |
c61b666e | 141 | if (m_ptr && !m_ptr->addr2str(a, addr_str, sizeof(addr_str))) |
dc1aed37 | 142 | ret = tipc_snprintf(buf, len, "%s(%s)", m_ptr->name, addr_str); |
c61b666e | 143 | else { |
c61b666e | 144 | u32 i; |
b97bf3fd | 145 | |
dc1aed37 | 146 | ret = tipc_snprintf(buf, len, "UNKNOWN(%u)", a->media_id); |
3d749a6a | 147 | for (i = 0; i < sizeof(a->value); i++) |
dc1aed37 EH |
148 | ret += tipc_snprintf(buf - ret, len + ret, |
149 | "-%02x", a->value[i]); | |
b97bf3fd PL |
150 | } |
151 | } | |
152 | ||
153 | /** | |
4323add6 | 154 | * tipc_media_get_names - record names of registered media in buffer |
b97bf3fd | 155 | */ |
4323add6 | 156 | struct sk_buff *tipc_media_get_names(void) |
b97bf3fd PL |
157 | { |
158 | struct sk_buff *buf; | |
b97bf3fd PL |
159 | int i; |
160 | ||
4323add6 | 161 | buf = tipc_cfg_reply_alloc(MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME)); |
b97bf3fd PL |
162 | if (!buf) |
163 | return NULL; | |
164 | ||
4323add6 | 165 | read_lock_bh(&tipc_net_lock); |
a31abe8d AS |
166 | for (i = 0; i < media_count; i++) { |
167 | tipc_cfg_append_tlv(buf, TIPC_TLV_MEDIA_NAME, | |
168 | media_list[i]->name, | |
169 | strlen(media_list[i]->name) + 1); | |
b97bf3fd | 170 | } |
4323add6 | 171 | read_unlock_bh(&tipc_net_lock); |
b97bf3fd PL |
172 | return buf; |
173 | } | |
174 | ||
175 | /** | |
176 | * bearer_name_validate - validate & (optionally) deconstruct bearer name | |
2c53040f BH |
177 | * @name: ptr to bearer name string |
178 | * @name_parts: ptr to area for bearer name components (or NULL if not needed) | |
c4307285 | 179 | * |
b97bf3fd PL |
180 | * Returns 1 if bearer name is valid, otherwise 0. |
181 | */ | |
c4307285 | 182 | static int bearer_name_validate(const char *name, |
f19765f4 | 183 | struct tipc_bearer_names *name_parts) |
b97bf3fd PL |
184 | { |
185 | char name_copy[TIPC_MAX_BEARER_NAME]; | |
186 | char *media_name; | |
187 | char *if_name; | |
188 | u32 media_len; | |
189 | u32 if_len; | |
190 | ||
191 | /* copy bearer name & ensure length is OK */ | |
b97bf3fd PL |
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) | |
196 | return 0; | |
197 | ||
198 | /* ensure all component parts of bearer name are present */ | |
b97bf3fd | 199 | media_name = name_copy; |
2db9983a AS |
200 | if_name = strchr(media_name, ':'); |
201 | if (if_name == NULL) | |
b97bf3fd PL |
202 | return 0; |
203 | *(if_name++) = 0; | |
204 | media_len = if_name - media_name; | |
205 | if_len = strlen(if_name) + 1; | |
206 | ||
207 | /* validate component parts of bearer name */ | |
c4307285 YH |
208 | if ((media_len <= 1) || (media_len > TIPC_MAX_MEDIA_NAME) || |
209 | (if_len <= 1) || (if_len > TIPC_MAX_IF_NAME) || | |
b97bf3fd PL |
210 | (strspn(media_name, tipc_alphabet) != (media_len - 1)) || |
211 | (strspn(if_name, tipc_alphabet) != (if_len - 1))) | |
212 | return 0; | |
213 | ||
214 | /* return bearer name components, if necessary */ | |
b97bf3fd PL |
215 | if (name_parts) { |
216 | strcpy(name_parts->media_name, media_name); | |
217 | strcpy(name_parts->if_name, if_name); | |
218 | } | |
219 | return 1; | |
220 | } | |
221 | ||
222 | /** | |
5c216e1d | 223 | * tipc_bearer_find - locates bearer object with matching bearer name |
b97bf3fd | 224 | */ |
5c216e1d | 225 | struct tipc_bearer *tipc_bearer_find(const char *name) |
b97bf3fd | 226 | { |
2d627b92 | 227 | struct tipc_bearer *b_ptr; |
b97bf3fd PL |
228 | u32 i; |
229 | ||
4323add6 | 230 | for (i = 0, b_ptr = tipc_bearers; i < MAX_BEARERS; i++, b_ptr++) { |
2d627b92 | 231 | if (b_ptr->active && (!strcmp(b_ptr->name, name))) |
b97bf3fd PL |
232 | return b_ptr; |
233 | } | |
1fc54d8f | 234 | return NULL; |
b97bf3fd PL |
235 | } |
236 | ||
237 | /** | |
4323add6 | 238 | * tipc_bearer_find_interface - locates bearer object with matching interface name |
b97bf3fd | 239 | */ |
2d627b92 | 240 | struct tipc_bearer *tipc_bearer_find_interface(const char *if_name) |
b97bf3fd | 241 | { |
2d627b92 | 242 | struct tipc_bearer *b_ptr; |
b97bf3fd PL |
243 | char *b_if_name; |
244 | u32 i; | |
245 | ||
4323add6 | 246 | for (i = 0, b_ptr = tipc_bearers; i < MAX_BEARERS; i++, b_ptr++) { |
b97bf3fd PL |
247 | if (!b_ptr->active) |
248 | continue; | |
2d627b92 | 249 | b_if_name = strchr(b_ptr->name, ':') + 1; |
b97bf3fd PL |
250 | if (!strcmp(b_if_name, if_name)) |
251 | return b_ptr; | |
252 | } | |
1fc54d8f | 253 | return NULL; |
b97bf3fd PL |
254 | } |
255 | ||
256 | /** | |
4323add6 | 257 | * tipc_bearer_get_names - record names of bearers in buffer |
b97bf3fd | 258 | */ |
4323add6 | 259 | struct sk_buff *tipc_bearer_get_names(void) |
b97bf3fd PL |
260 | { |
261 | struct sk_buff *buf; | |
2d627b92 | 262 | struct tipc_bearer *b_ptr; |
b97bf3fd PL |
263 | int i, j; |
264 | ||
4323add6 | 265 | buf = tipc_cfg_reply_alloc(MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME)); |
b97bf3fd PL |
266 | if (!buf) |
267 | return NULL; | |
268 | ||
4323add6 | 269 | read_lock_bh(&tipc_net_lock); |
a31abe8d | 270 | for (i = 0; i < media_count; i++) { |
b97bf3fd | 271 | for (j = 0; j < MAX_BEARERS; j++) { |
4323add6 | 272 | b_ptr = &tipc_bearers[j]; |
a31abe8d | 273 | if (b_ptr->active && (b_ptr->media == media_list[i])) { |
c4307285 | 274 | tipc_cfg_append_tlv(buf, TIPC_TLV_BEARER_NAME, |
2d627b92 AS |
275 | b_ptr->name, |
276 | strlen(b_ptr->name) + 1); | |
b97bf3fd PL |
277 | } |
278 | } | |
279 | } | |
4323add6 | 280 | read_unlock_bh(&tipc_net_lock); |
b97bf3fd PL |
281 | return buf; |
282 | } | |
283 | ||
2d627b92 | 284 | void tipc_bearer_add_dest(struct tipc_bearer *b_ptr, u32 dest) |
b97bf3fd | 285 | { |
4323add6 | 286 | tipc_nmap_add(&b_ptr->nodes, dest); |
4323add6 | 287 | tipc_bcbearer_sort(); |
1209966c | 288 | tipc_disc_add_dest(b_ptr->link_req); |
b97bf3fd PL |
289 | } |
290 | ||
2d627b92 | 291 | void tipc_bearer_remove_dest(struct tipc_bearer *b_ptr, u32 dest) |
b97bf3fd | 292 | { |
4323add6 | 293 | tipc_nmap_remove(&b_ptr->nodes, dest); |
4323add6 | 294 | tipc_bcbearer_sort(); |
1209966c | 295 | tipc_disc_remove_dest(b_ptr->link_req); |
b97bf3fd PL |
296 | } |
297 | ||
298 | /* | |
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. | |
4323add6 | 302 | * 'tipc_net_lock' is read_locked when this function is called |
b97bf3fd PL |
303 | * bearer.lock must be taken before calling |
304 | * Returns binary true(1) ore false(0) | |
305 | */ | |
2d627b92 | 306 | static int bearer_push(struct tipc_bearer *b_ptr) |
b97bf3fd | 307 | { |
0e35fd5e | 308 | u32 res = 0; |
a18c4bc3 | 309 | struct tipc_link *ln, *tln; |
b97bf3fd | 310 | |
2d627b92 | 311 | if (b_ptr->blocked) |
b97bf3fd PL |
312 | return 0; |
313 | ||
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) { | |
4323add6 | 316 | res = tipc_link_push_packet(ln); |
b97bf3fd PL |
317 | if (res == PUSH_FAILED) |
318 | break; | |
319 | if (res == PUSH_FINISHED) | |
320 | list_move_tail(&ln->link_list, &b_ptr->links); | |
321 | } | |
322 | } | |
323 | return list_empty(&b_ptr->cong_links); | |
324 | } | |
325 | ||
2d627b92 | 326 | void tipc_bearer_lock_push(struct tipc_bearer *b_ptr) |
b97bf3fd | 327 | { |
2d627b92 | 328 | spin_lock_bh(&b_ptr->lock); |
23f0ff90 | 329 | bearer_push(b_ptr); |
2d627b92 | 330 | spin_unlock_bh(&b_ptr->lock); |
b97bf3fd PL |
331 | } |
332 | ||
333 | ||
334 | /* | |
c4307285 YH |
335 | * Interrupt enabling new requests after bearer congestion or blocking: |
336 | * See bearer_send(). | |
b97bf3fd | 337 | */ |
2d627b92 | 338 | void tipc_continue(struct tipc_bearer *b_ptr) |
b97bf3fd | 339 | { |
2d627b92 | 340 | spin_lock_bh(&b_ptr->lock); |
b97bf3fd | 341 | if (!list_empty(&b_ptr->cong_links)) |
4323add6 | 342 | tipc_k_signal((Handler)tipc_bearer_lock_push, (unsigned long)b_ptr); |
2d627b92 AS |
343 | b_ptr->blocked = 0; |
344 | spin_unlock_bh(&b_ptr->lock); | |
b97bf3fd PL |
345 | } |
346 | ||
347 | /* | |
c4307285 YH |
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 | |
4323add6 | 351 | * the bearer is congested. 'tipc_net_lock' is in read_lock here |
b97bf3fd PL |
352 | * bearer.lock is busy |
353 | */ | |
a18c4bc3 PG |
354 | static void tipc_bearer_schedule_unlocked(struct tipc_bearer *b_ptr, |
355 | struct tipc_link *l_ptr) | |
b97bf3fd PL |
356 | { |
357 | list_move_tail(&l_ptr->link_list, &b_ptr->cong_links); | |
358 | } | |
359 | ||
360 | /* | |
c4307285 YH |
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 | |
4323add6 | 364 | * the bearer is congested. 'tipc_net_lock' is in read_lock here, |
b97bf3fd PL |
365 | * bearer.lock is free |
366 | */ | |
a18c4bc3 | 367 | void tipc_bearer_schedule(struct tipc_bearer *b_ptr, struct tipc_link *l_ptr) |
b97bf3fd | 368 | { |
2d627b92 | 369 | spin_lock_bh(&b_ptr->lock); |
4323add6 | 370 | tipc_bearer_schedule_unlocked(b_ptr, l_ptr); |
2d627b92 | 371 | spin_unlock_bh(&b_ptr->lock); |
b97bf3fd PL |
372 | } |
373 | ||
374 | ||
375 | /* | |
4323add6 | 376 | * tipc_bearer_resolve_congestion(): Check if there is bearer congestion, |
b97bf3fd | 377 | * and if there is, try to resolve it before returning. |
4323add6 | 378 | * 'tipc_net_lock' is read_locked when this function is called |
b97bf3fd | 379 | */ |
a18c4bc3 PG |
380 | int tipc_bearer_resolve_congestion(struct tipc_bearer *b_ptr, |
381 | struct tipc_link *l_ptr) | |
b97bf3fd PL |
382 | { |
383 | int res = 1; | |
384 | ||
385 | if (list_empty(&b_ptr->cong_links)) | |
386 | return 1; | |
2d627b92 | 387 | spin_lock_bh(&b_ptr->lock); |
b97bf3fd | 388 | if (!bearer_push(b_ptr)) { |
4323add6 | 389 | tipc_bearer_schedule_unlocked(b_ptr, l_ptr); |
b97bf3fd PL |
390 | res = 0; |
391 | } | |
2d627b92 | 392 | spin_unlock_bh(&b_ptr->lock); |
b97bf3fd PL |
393 | return res; |
394 | } | |
395 | ||
b274f4ab AS |
396 | /** |
397 | * tipc_bearer_congested - determines if bearer is currently congested | |
398 | */ | |
a18c4bc3 | 399 | int tipc_bearer_congested(struct tipc_bearer *b_ptr, struct tipc_link *l_ptr) |
b274f4ab | 400 | { |
2d627b92 | 401 | if (unlikely(b_ptr->blocked)) |
b274f4ab AS |
402 | return 1; |
403 | if (likely(list_empty(&b_ptr->cong_links))) | |
404 | return 0; | |
405 | return !tipc_bearer_resolve_congestion(b_ptr, l_ptr); | |
406 | } | |
b97bf3fd PL |
407 | |
408 | /** | |
409 | * tipc_enable_bearer - enable bearer with the given name | |
c4307285 | 410 | */ |
50d3e639 | 411 | int tipc_enable_bearer(const char *name, u32 disc_domain, u32 priority) |
b97bf3fd | 412 | { |
2d627b92 | 413 | struct tipc_bearer *b_ptr; |
358a0d1c | 414 | struct tipc_media *m_ptr; |
f19765f4 | 415 | struct tipc_bearer_names b_names; |
b97bf3fd PL |
416 | char addr_string[16]; |
417 | u32 bearer_id; | |
418 | u32 with_this_prio; | |
419 | u32 i; | |
420 | int res = -EINVAL; | |
421 | ||
b58343f9 | 422 | if (!tipc_own_addr) { |
2cf8aa19 EH |
423 | pr_warn("Bearer <%s> rejected, not supported in standalone mode\n", |
424 | name); | |
b97bf3fd | 425 | return -ENOPROTOOPT; |
a10bd924 | 426 | } |
f19765f4 | 427 | if (!bearer_name_validate(name, &b_names)) { |
2cf8aa19 | 428 | pr_warn("Bearer <%s> rejected, illegal name\n", name); |
16cb4b33 | 429 | return -EINVAL; |
a10bd924 | 430 | } |
66e019a6 AS |
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 */ | |
336ebf5b | 436 | } else if (in_own_cluster_exact(disc_domain)) |
66e019a6 AS |
437 | res = 0; /* accept specified node in own cluster */ |
438 | } | |
439 | if (res) { | |
2cf8aa19 EH |
440 | pr_warn("Bearer <%s> rejected, illegal discovery domain\n", |
441 | name); | |
a10bd924 AS |
442 | return -EINVAL; |
443 | } | |
9efde4a0 | 444 | if ((priority > TIPC_MAX_LINK_PRI) && |
a10bd924 | 445 | (priority != TIPC_MEDIA_LINK_PRI)) { |
2cf8aa19 | 446 | pr_warn("Bearer <%s> rejected, illegal priority\n", name); |
b97bf3fd | 447 | return -EINVAL; |
a10bd924 | 448 | } |
b97bf3fd | 449 | |
4323add6 | 450 | write_lock_bh(&tipc_net_lock); |
b97bf3fd | 451 | |
f19765f4 | 452 | m_ptr = tipc_media_find(b_names.media_name); |
b97bf3fd | 453 | if (!m_ptr) { |
2cf8aa19 EH |
454 | pr_warn("Bearer <%s> rejected, media <%s> not registered\n", |
455 | name, b_names.media_name); | |
3a777ff8 | 456 | goto exit; |
b97bf3fd | 457 | } |
16cb4b33 PL |
458 | |
459 | if (priority == TIPC_MEDIA_LINK_PRI) | |
b97bf3fd PL |
460 | priority = m_ptr->priority; |
461 | ||
462 | restart: | |
463 | bearer_id = MAX_BEARERS; | |
464 | with_this_prio = 1; | |
465 | for (i = MAX_BEARERS; i-- != 0; ) { | |
4323add6 | 466 | if (!tipc_bearers[i].active) { |
b97bf3fd PL |
467 | bearer_id = i; |
468 | continue; | |
469 | } | |
2d627b92 | 470 | if (!strcmp(name, tipc_bearers[i].name)) { |
2cf8aa19 EH |
471 | pr_warn("Bearer <%s> rejected, already enabled\n", |
472 | name); | |
3a777ff8 | 473 | goto exit; |
b97bf3fd | 474 | } |
4323add6 | 475 | if ((tipc_bearers[i].priority == priority) && |
b97bf3fd PL |
476 | (++with_this_prio > 2)) { |
477 | if (priority-- == 0) { | |
2cf8aa19 EH |
478 | pr_warn("Bearer <%s> rejected, duplicate priority\n", |
479 | name); | |
3a777ff8 | 480 | goto exit; |
b97bf3fd | 481 | } |
2cf8aa19 EH |
482 | pr_warn("Bearer <%s> priority adjustment required %u->%u\n", |
483 | name, priority + 1, priority); | |
b97bf3fd PL |
484 | goto restart; |
485 | } | |
486 | } | |
487 | if (bearer_id >= MAX_BEARERS) { | |
2cf8aa19 EH |
488 | pr_warn("Bearer <%s> rejected, bearer limit reached (%u)\n", |
489 | name, MAX_BEARERS); | |
3a777ff8 | 490 | goto exit; |
b97bf3fd PL |
491 | } |
492 | ||
4323add6 | 493 | b_ptr = &tipc_bearers[bearer_id]; |
2d627b92 AS |
494 | strcpy(b_ptr->name, name); |
495 | res = m_ptr->enable_bearer(b_ptr); | |
b97bf3fd | 496 | if (res) { |
2cf8aa19 EH |
497 | pr_warn("Bearer <%s> rejected, enable failure (%d)\n", |
498 | name, -res); | |
3a777ff8 | 499 | goto exit; |
b97bf3fd PL |
500 | } |
501 | ||
502 | b_ptr->identity = bearer_id; | |
503 | b_ptr->media = m_ptr; | |
5c216e1d AS |
504 | b_ptr->tolerance = m_ptr->tolerance; |
505 | b_ptr->window = m_ptr->window; | |
b97bf3fd PL |
506 | b_ptr->net_plane = bearer_id + 'A'; |
507 | b_ptr->active = 1; | |
b97bf3fd PL |
508 | b_ptr->priority = priority; |
509 | INIT_LIST_HEAD(&b_ptr->cong_links); | |
510 | INIT_LIST_HEAD(&b_ptr->links); | |
2d627b92 | 511 | spin_lock_init(&b_ptr->lock); |
3a777ff8 AS |
512 | |
513 | res = tipc_disc_create(b_ptr, &m_ptr->bcast_addr, disc_domain); | |
514 | if (res) { | |
515 | bearer_disable(b_ptr); | |
2cf8aa19 EH |
516 | pr_warn("Bearer <%s> rejected, discovery object creation failed\n", |
517 | name); | |
3a777ff8 AS |
518 | goto exit; |
519 | } | |
2cf8aa19 EH |
520 | pr_info("Enabled bearer <%s>, discovery domain %s, priority %u\n", |
521 | name, | |
522 | tipc_addr_string_fill(addr_string, disc_domain), priority); | |
3a777ff8 | 523 | exit: |
4323add6 | 524 | write_unlock_bh(&tipc_net_lock); |
b97bf3fd PL |
525 | return res; |
526 | } | |
527 | ||
528 | /** | |
2c53040f | 529 | * tipc_block_bearer - Block the bearer with the given name, and reset all its links |
b97bf3fd | 530 | */ |
b97bf3fd PL |
531 | int tipc_block_bearer(const char *name) |
532 | { | |
2d627b92 | 533 | struct tipc_bearer *b_ptr = NULL; |
a18c4bc3 PG |
534 | struct tipc_link *l_ptr; |
535 | struct tipc_link *temp_l_ptr; | |
b97bf3fd | 536 | |
4323add6 | 537 | read_lock_bh(&tipc_net_lock); |
5c216e1d | 538 | b_ptr = tipc_bearer_find(name); |
b97bf3fd | 539 | if (!b_ptr) { |
2cf8aa19 | 540 | pr_warn("Attempt to block unknown bearer <%s>\n", name); |
4323add6 | 541 | read_unlock_bh(&tipc_net_lock); |
b97bf3fd PL |
542 | return -EINVAL; |
543 | } | |
544 | ||
2cf8aa19 | 545 | pr_info("Blocking bearer <%s>\n", name); |
2d627b92 AS |
546 | spin_lock_bh(&b_ptr->lock); |
547 | b_ptr->blocked = 1; | |
4b3743ef | 548 | list_splice_init(&b_ptr->cong_links, &b_ptr->links); |
b97bf3fd | 549 | list_for_each_entry_safe(l_ptr, temp_l_ptr, &b_ptr->links, link_list) { |
6c00055a | 550 | struct tipc_node *n_ptr = l_ptr->owner; |
b97bf3fd PL |
551 | |
552 | spin_lock_bh(&n_ptr->lock); | |
4323add6 | 553 | tipc_link_reset(l_ptr); |
b97bf3fd PL |
554 | spin_unlock_bh(&n_ptr->lock); |
555 | } | |
2d627b92 | 556 | spin_unlock_bh(&b_ptr->lock); |
4323add6 | 557 | read_unlock_bh(&tipc_net_lock); |
0e35fd5e | 558 | return 0; |
b97bf3fd PL |
559 | } |
560 | ||
561 | /** | |
617d3c7a | 562 | * bearer_disable |
c4307285 | 563 | * |
4323add6 | 564 | * Note: This routine assumes caller holds tipc_net_lock. |
b97bf3fd | 565 | */ |
2d627b92 | 566 | static void bearer_disable(struct tipc_bearer *b_ptr) |
b97bf3fd | 567 | { |
a18c4bc3 PG |
568 | struct tipc_link *l_ptr; |
569 | struct tipc_link *temp_l_ptr; | |
b97bf3fd | 570 | |
2cf8aa19 | 571 | pr_info("Disabling bearer <%s>\n", b_ptr->name); |
2d627b92 | 572 | spin_lock_bh(&b_ptr->lock); |
2d627b92 AS |
573 | b_ptr->blocked = 1; |
574 | b_ptr->media->disable_bearer(b_ptr); | |
4b3743ef | 575 | list_splice_init(&b_ptr->cong_links, &b_ptr->links); |
b97bf3fd | 576 | list_for_each_entry_safe(l_ptr, temp_l_ptr, &b_ptr->links, link_list) { |
4323add6 | 577 | tipc_link_delete(l_ptr); |
b97bf3fd | 578 | } |
3a777ff8 AS |
579 | if (b_ptr->link_req) |
580 | tipc_disc_delete(b_ptr->link_req); | |
2d627b92 AS |
581 | spin_unlock_bh(&b_ptr->lock); |
582 | memset(b_ptr, 0, sizeof(struct tipc_bearer)); | |
b97bf3fd PL |
583 | } |
584 | ||
585 | int tipc_disable_bearer(const char *name) | |
586 | { | |
2d627b92 | 587 | struct tipc_bearer *b_ptr; |
b97bf3fd PL |
588 | int res; |
589 | ||
4323add6 | 590 | write_lock_bh(&tipc_net_lock); |
5c216e1d | 591 | b_ptr = tipc_bearer_find(name); |
ccc901ee | 592 | if (b_ptr == NULL) { |
2cf8aa19 | 593 | pr_warn("Attempt to disable unknown bearer <%s>\n", name); |
ccc901ee | 594 | res = -EINVAL; |
28cc937e AS |
595 | } else { |
596 | bearer_disable(b_ptr); | |
597 | res = 0; | |
598 | } | |
4323add6 | 599 | write_unlock_bh(&tipc_net_lock); |
b97bf3fd PL |
600 | return res; |
601 | } | |
602 | ||
603 | ||
604 | ||
4323add6 | 605 | void tipc_bearer_stop(void) |
b97bf3fd PL |
606 | { |
607 | u32 i; | |
608 | ||
b97bf3fd | 609 | for (i = 0; i < MAX_BEARERS; i++) { |
4323add6 | 610 | if (tipc_bearers[i].active) |
ccc901ee | 611 | bearer_disable(&tipc_bearers[i]); |
b97bf3fd | 612 | } |
b97bf3fd PL |
613 | media_count = 0; |
614 | } |