bearer.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /*
  2. * net/tipc/bearer.c: TIPC bearer code
  3. *
  4. * Copyright (c) 2003-2005, Ericsson Research Canada
  5. * Copyright (c) 2004-2005, Wind River Systems
  6. * Copyright (c) 2005-2006, Ericsson AB
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the names of the copyright holders nor the names of its
  18. * contributors may be used to endorse or promote products derived from
  19. * this software without specific prior written permission.
  20. *
  21. * Alternatively, this software may be distributed under the terms of the
  22. * GNU General Public License ("GPL") version 2 as published by the Free
  23. * Software Foundation.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include "core.h"
  38. #include "config.h"
  39. #include "dbg.h"
  40. #include "bearer.h"
  41. #include "link.h"
  42. #include "port.h"
  43. #include "discover.h"
  44. #include "bcast.h"
  45. #define MAX_ADDR_STR 32
  46. static struct media *media_list = 0;
  47. static u32 media_count = 0;
  48. struct bearer *bearers = 0;
  49. /**
  50. * media_name_valid - validate media name
  51. *
  52. * Returns 1 if media name is valid, otherwise 0.
  53. */
  54. static int media_name_valid(const char *name)
  55. {
  56. u32 len;
  57. len = strlen(name);
  58. if ((len + 1) > TIPC_MAX_MEDIA_NAME)
  59. return 0;
  60. return (strspn(name, tipc_alphabet) == len);
  61. }
  62. /**
  63. * media_find - locates specified media object by name
  64. */
  65. static struct media *media_find(const char *name)
  66. {
  67. struct media *m_ptr;
  68. u32 i;
  69. for (i = 0, m_ptr = media_list; i < media_count; i++, m_ptr++) {
  70. if (!strcmp(m_ptr->name, name))
  71. return m_ptr;
  72. }
  73. return 0;
  74. }
  75. /**
  76. * tipc_register_media - register a media type
  77. *
  78. * Bearers for this media type must be activated separately at a later stage.
  79. */
  80. int tipc_register_media(u32 media_type,
  81. char *name,
  82. int (*enable)(struct tipc_bearer *),
  83. void (*disable)(struct tipc_bearer *),
  84. int (*send_msg)(struct sk_buff *,
  85. struct tipc_bearer *,
  86. struct tipc_media_addr *),
  87. char *(*addr2str)(struct tipc_media_addr *a,
  88. char *str_buf, int str_size),
  89. struct tipc_media_addr *bcast_addr,
  90. const u32 bearer_priority,
  91. const u32 link_tolerance, /* [ms] */
  92. const u32 send_window_limit)
  93. {
  94. struct media *m_ptr;
  95. u32 media_id;
  96. u32 i;
  97. int res = -EINVAL;
  98. write_lock_bh(&net_lock);
  99. if (!media_list)
  100. goto exit;
  101. if (!media_name_valid(name)) {
  102. warn("Media registration error: illegal name <%s>\n", name);
  103. goto exit;
  104. }
  105. if (!bcast_addr) {
  106. warn("Media registration error: no broadcast address supplied\n");
  107. goto exit;
  108. }
  109. if (bearer_priority >= TIPC_NUM_LINK_PRI) {
  110. warn("Media registration error: priority %u\n", bearer_priority);
  111. goto exit;
  112. }
  113. if ((link_tolerance < TIPC_MIN_LINK_TOL) ||
  114. (link_tolerance > TIPC_MAX_LINK_TOL)) {
  115. warn("Media registration error: tolerance %u\n", link_tolerance);
  116. goto exit;
  117. }
  118. media_id = media_count++;
  119. if (media_id >= MAX_MEDIA) {
  120. warn("Attempt to register more than %u media\n", MAX_MEDIA);
  121. media_count--;
  122. goto exit;
  123. }
  124. for (i = 0; i < media_id; i++) {
  125. if (media_list[i].type_id == media_type) {
  126. warn("Attempt to register second media with type %u\n",
  127. media_type);
  128. media_count--;
  129. goto exit;
  130. }
  131. if (!strcmp(name, media_list[i].name)) {
  132. warn("Attempt to re-register media name <%s>\n", name);
  133. media_count--;
  134. goto exit;
  135. }
  136. }
  137. m_ptr = &media_list[media_id];
  138. m_ptr->type_id = media_type;
  139. m_ptr->send_msg = send_msg;
  140. m_ptr->enable_bearer = enable;
  141. m_ptr->disable_bearer = disable;
  142. m_ptr->addr2str = addr2str;
  143. memcpy(&m_ptr->bcast_addr, bcast_addr, sizeof(*bcast_addr));
  144. m_ptr->bcast = 1;
  145. strcpy(m_ptr->name, name);
  146. m_ptr->priority = bearer_priority;
  147. m_ptr->tolerance = link_tolerance;
  148. m_ptr->window = send_window_limit;
  149. dbg("Media <%s> registered\n", name);
  150. res = 0;
  151. exit:
  152. write_unlock_bh(&net_lock);
  153. return res;
  154. }
  155. /**
  156. * media_addr_printf - record media address in print buffer
  157. */
  158. void 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. media_type = ntohl(a->type);
  164. for (i = 0, m_ptr = media_list; i < media_count; i++, m_ptr++) {
  165. if (m_ptr->type_id == media_type)
  166. break;
  167. }
  168. if ((i < media_count) && (m_ptr->addr2str != NULL)) {
  169. char addr_str[MAX_ADDR_STR];
  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. tipc_printf(pb, "UNKNOWN(%u):", media_type);
  175. for (i = 0; i < (sizeof(*a) - sizeof(a->type)); i++) {
  176. tipc_printf(pb, "%02x ", addr[i]);
  177. }
  178. }
  179. }
  180. /**
  181. * media_get_names - record names of registered media in buffer
  182. */
  183. struct sk_buff *media_get_names(void)
  184. {
  185. struct sk_buff *buf;
  186. struct media *m_ptr;
  187. int i;
  188. buf = cfg_reply_alloc(MAX_MEDIA * TLV_SPACE(TIPC_MAX_MEDIA_NAME));
  189. if (!buf)
  190. return NULL;
  191. read_lock_bh(&net_lock);
  192. for (i = 0, m_ptr = media_list; i < media_count; i++, m_ptr++) {
  193. cfg_append_tlv(buf, TIPC_TLV_MEDIA_NAME, m_ptr->name,
  194. strlen(m_ptr->name) + 1);
  195. }
  196. read_unlock_bh(&net_lock);
  197. return buf;
  198. }
  199. /**
  200. * bearer_name_validate - validate & (optionally) deconstruct bearer name
  201. * @name - ptr to bearer name string
  202. * @name_parts - ptr to area for bearer name components (or NULL if not needed)
  203. *
  204. * Returns 1 if bearer name is valid, otherwise 0.
  205. */
  206. static int bearer_name_validate(const char *name,
  207. struct bearer_name *name_parts)
  208. {
  209. char name_copy[TIPC_MAX_BEARER_NAME];
  210. char *media_name;
  211. char *if_name;
  212. u32 media_len;
  213. u32 if_len;
  214. /* copy bearer name & ensure length is OK */
  215. name_copy[TIPC_MAX_BEARER_NAME - 1] = 0;
  216. /* need above in case non-Posix strncpy() doesn't pad with nulls */
  217. strncpy(name_copy, name, TIPC_MAX_BEARER_NAME);
  218. if (name_copy[TIPC_MAX_BEARER_NAME - 1] != 0)
  219. return 0;
  220. /* ensure all component parts of bearer name are present */
  221. media_name = name_copy;
  222. if ((if_name = strchr(media_name, ':')) == NULL)
  223. return 0;
  224. *(if_name++) = 0;
  225. media_len = if_name - media_name;
  226. if_len = strlen(if_name) + 1;
  227. /* validate component parts of bearer name */
  228. if ((media_len <= 1) || (media_len > TIPC_MAX_MEDIA_NAME) ||
  229. (if_len <= 1) || (if_len > TIPC_MAX_IF_NAME) ||
  230. (strspn(media_name, tipc_alphabet) != (media_len - 1)) ||
  231. (strspn(if_name, tipc_alphabet) != (if_len - 1)))
  232. return 0;
  233. /* return bearer name components, if necessary */
  234. if (name_parts) {
  235. strcpy(name_parts->media_name, media_name);
  236. strcpy(name_parts->if_name, if_name);
  237. }
  238. return 1;
  239. }
  240. /**
  241. * bearer_find - locates bearer object with matching bearer name
  242. */
  243. static struct bearer *bearer_find(const char *name)
  244. {
  245. struct bearer *b_ptr;
  246. u32 i;
  247. for (i = 0, b_ptr = bearers; i < MAX_BEARERS; i++, b_ptr++) {
  248. if (b_ptr->active && (!strcmp(b_ptr->publ.name, name)))
  249. return b_ptr;
  250. }
  251. return 0;
  252. }
  253. /**
  254. * bearer_find - locates bearer object with matching interface name
  255. */
  256. struct bearer *bearer_find_interface(const char *if_name)
  257. {
  258. struct bearer *b_ptr;
  259. char *b_if_name;
  260. u32 i;
  261. for (i = 0, b_ptr = bearers; i < MAX_BEARERS; i++, b_ptr++) {
  262. if (!b_ptr->active)
  263. continue;
  264. b_if_name = strchr(b_ptr->publ.name, ':') + 1;
  265. if (!strcmp(b_if_name, if_name))
  266. return b_ptr;
  267. }
  268. return 0;
  269. }
  270. /**
  271. * bearer_get_names - record names of bearers in buffer
  272. */
  273. struct sk_buff *bearer_get_names(void)
  274. {
  275. struct sk_buff *buf;
  276. struct media *m_ptr;
  277. struct bearer *b_ptr;
  278. int i, j;
  279. buf = cfg_reply_alloc(MAX_BEARERS * TLV_SPACE(TIPC_MAX_BEARER_NAME));
  280. if (!buf)
  281. return NULL;
  282. read_lock_bh(&net_lock);
  283. for (i = 0, m_ptr = media_list; i < media_count; i++, m_ptr++) {
  284. for (j = 0; j < MAX_BEARERS; j++) {
  285. b_ptr = &bearers[j];
  286. if (b_ptr->active && (b_ptr->media == m_ptr)) {
  287. cfg_append_tlv(buf, TIPC_TLV_BEARER_NAME,
  288. b_ptr->publ.name,
  289. strlen(b_ptr->publ.name) + 1);
  290. }
  291. }
  292. }
  293. read_unlock_bh(&net_lock);
  294. return buf;
  295. }
  296. void bearer_add_dest(struct bearer *b_ptr, u32 dest)
  297. {
  298. nmap_add(&b_ptr->nodes, dest);
  299. disc_update_link_req(b_ptr->link_req);
  300. bcbearer_sort();
  301. }
  302. void bearer_remove_dest(struct bearer *b_ptr, u32 dest)
  303. {
  304. nmap_remove(&b_ptr->nodes, dest);
  305. disc_update_link_req(b_ptr->link_req);
  306. bcbearer_sort();
  307. }
  308. /*
  309. * bearer_push(): Resolve bearer congestion. Force the waiting
  310. * links to push out their unsent packets, one packet per link
  311. * per iteration, until all packets are gone or congestion reoccurs.
  312. * 'net_lock' is read_locked when this function is called
  313. * bearer.lock must be taken before calling
  314. * Returns binary true(1) ore false(0)
  315. */
  316. static int bearer_push(struct bearer *b_ptr)
  317. {
  318. u32 res = TIPC_OK;
  319. struct link *ln, *tln;
  320. if (b_ptr->publ.blocked)
  321. return 0;
  322. while (!list_empty(&b_ptr->cong_links) && (res != PUSH_FAILED)) {
  323. list_for_each_entry_safe(ln, tln, &b_ptr->cong_links, link_list) {
  324. res = link_push_packet(ln);
  325. if (res == PUSH_FAILED)
  326. break;
  327. if (res == PUSH_FINISHED)
  328. list_move_tail(&ln->link_list, &b_ptr->links);
  329. }
  330. }
  331. return list_empty(&b_ptr->cong_links);
  332. }
  333. void bearer_lock_push(struct bearer *b_ptr)
  334. {
  335. int res;
  336. spin_lock_bh(&b_ptr->publ.lock);
  337. res = bearer_push(b_ptr);
  338. spin_unlock_bh(&b_ptr->publ.lock);
  339. if (res)
  340. bcbearer_push();
  341. }
  342. /*
  343. * Interrupt enabling new requests after bearer congestion or blocking:
  344. * See bearer_send().
  345. */
  346. void tipc_continue(struct tipc_bearer *tb_ptr)
  347. {
  348. struct bearer *b_ptr = (struct bearer *)tb_ptr;
  349. spin_lock_bh(&b_ptr->publ.lock);
  350. b_ptr->continue_count++;
  351. if (!list_empty(&b_ptr->cong_links))
  352. k_signal((Handler)bearer_lock_push, (unsigned long)b_ptr);
  353. b_ptr->publ.blocked = 0;
  354. spin_unlock_bh(&b_ptr->publ.lock);
  355. }
  356. /*
  357. * Schedule link for sending of messages after the bearer
  358. * has been deblocked by 'continue()'. This method is called
  359. * when somebody tries to send a message via this link while
  360. * the bearer is congested. 'net_lock' is in read_lock here
  361. * bearer.lock is busy
  362. */
  363. static void bearer_schedule_unlocked(struct bearer *b_ptr, struct link *l_ptr)
  364. {
  365. list_move_tail(&l_ptr->link_list, &b_ptr->cong_links);
  366. }
  367. /*
  368. * Schedule link for sending of messages after the bearer
  369. * has been deblocked by 'continue()'. This method is called
  370. * when somebody tries to send a message via this link while
  371. * the bearer is congested. 'net_lock' is in read_lock here,
  372. * bearer.lock is free
  373. */
  374. void bearer_schedule(struct bearer *b_ptr, struct link *l_ptr)
  375. {
  376. spin_lock_bh(&b_ptr->publ.lock);
  377. bearer_schedule_unlocked(b_ptr, l_ptr);
  378. spin_unlock_bh(&b_ptr->publ.lock);
  379. }
  380. /*
  381. * bearer_resolve_congestion(): Check if there is bearer congestion,
  382. * and if there is, try to resolve it before returning.
  383. * 'net_lock' is read_locked when this function is called
  384. */
  385. int bearer_resolve_congestion(struct bearer *b_ptr, struct link *l_ptr)
  386. {
  387. int res = 1;
  388. if (list_empty(&b_ptr->cong_links))
  389. return 1;
  390. spin_lock_bh(&b_ptr->publ.lock);
  391. if (!bearer_push(b_ptr)) {
  392. bearer_schedule_unlocked(b_ptr, l_ptr);
  393. res = 0;
  394. }
  395. spin_unlock_bh(&b_ptr->publ.lock);
  396. return res;
  397. }
  398. /**
  399. * tipc_enable_bearer - enable bearer with the given name
  400. */
  401. int tipc_enable_bearer(const char *name, u32 bcast_scope, u32 priority)
  402. {
  403. struct bearer *b_ptr;
  404. struct media *m_ptr;
  405. struct bearer_name b_name;
  406. char addr_string[16];
  407. u32 bearer_id;
  408. u32 with_this_prio;
  409. u32 i;
  410. int res = -EINVAL;
  411. if (tipc_mode != TIPC_NET_MODE)
  412. return -ENOPROTOOPT;
  413. if (!bearer_name_validate(name, &b_name) ||
  414. !addr_domain_valid(bcast_scope) ||
  415. !in_scope(bcast_scope, tipc_own_addr) ||
  416. (priority > TIPC_NUM_LINK_PRI))
  417. return -EINVAL;
  418. write_lock_bh(&net_lock);
  419. if (!bearers)
  420. goto failed;
  421. m_ptr = media_find(b_name.media_name);
  422. if (!m_ptr) {
  423. warn("No media <%s>\n", b_name.media_name);
  424. goto failed;
  425. }
  426. if (priority == TIPC_NUM_LINK_PRI)
  427. priority = m_ptr->priority;
  428. restart:
  429. bearer_id = MAX_BEARERS;
  430. with_this_prio = 1;
  431. for (i = MAX_BEARERS; i-- != 0; ) {
  432. if (!bearers[i].active) {
  433. bearer_id = i;
  434. continue;
  435. }
  436. if (!strcmp(name, bearers[i].publ.name)) {
  437. warn("Bearer <%s> already enabled\n", name);
  438. goto failed;
  439. }
  440. if ((bearers[i].priority == priority) &&
  441. (++with_this_prio > 2)) {
  442. if (priority-- == 0) {
  443. warn("Third bearer <%s> with priority %u, unable to lower to %u\n",
  444. name, priority + 1, priority);
  445. goto failed;
  446. }
  447. warn("Third bearer <%s> with priority %u, lowering to %u\n",
  448. name, priority + 1, priority);
  449. goto restart;
  450. }
  451. }
  452. if (bearer_id >= MAX_BEARERS) {
  453. warn("Attempt to enable more than %d bearers\n", MAX_BEARERS);
  454. goto failed;
  455. }
  456. b_ptr = &bearers[bearer_id];
  457. memset(b_ptr, 0, sizeof(struct bearer));
  458. strcpy(b_ptr->publ.name, name);
  459. res = m_ptr->enable_bearer(&b_ptr->publ);
  460. if (res) {
  461. warn("Failed to enable bearer <%s>\n", name);
  462. goto failed;
  463. }
  464. b_ptr->identity = bearer_id;
  465. b_ptr->media = m_ptr;
  466. b_ptr->net_plane = bearer_id + 'A';
  467. b_ptr->active = 1;
  468. b_ptr->detect_scope = bcast_scope;
  469. b_ptr->priority = priority;
  470. INIT_LIST_HEAD(&b_ptr->cong_links);
  471. INIT_LIST_HEAD(&b_ptr->links);
  472. if (m_ptr->bcast) {
  473. b_ptr->link_req = disc_init_link_req(b_ptr, &m_ptr->bcast_addr,
  474. bcast_scope, 2);
  475. }
  476. b_ptr->publ.lock = SPIN_LOCK_UNLOCKED;
  477. write_unlock_bh(&net_lock);
  478. info("Enabled bearer <%s>, discovery domain %s\n",
  479. name, addr_string_fill(addr_string, bcast_scope));
  480. return 0;
  481. failed:
  482. write_unlock_bh(&net_lock);
  483. return res;
  484. }
  485. /**
  486. * tipc_block_bearer(): Block the bearer with the given name,
  487. * and reset all its links
  488. */
  489. int tipc_block_bearer(const char *name)
  490. {
  491. struct bearer *b_ptr = 0;
  492. struct link *l_ptr;
  493. struct link *temp_l_ptr;
  494. if (tipc_mode != TIPC_NET_MODE)
  495. return -ENOPROTOOPT;
  496. read_lock_bh(&net_lock);
  497. b_ptr = bearer_find(name);
  498. if (!b_ptr) {
  499. warn("Attempt to block unknown bearer <%s>\n", name);
  500. read_unlock_bh(&net_lock);
  501. return -EINVAL;
  502. }
  503. spin_lock_bh(&b_ptr->publ.lock);
  504. b_ptr->publ.blocked = 1;
  505. list_for_each_entry_safe(l_ptr, temp_l_ptr, &b_ptr->links, link_list) {
  506. struct node *n_ptr = l_ptr->owner;
  507. spin_lock_bh(&n_ptr->lock);
  508. link_reset(l_ptr);
  509. spin_unlock_bh(&n_ptr->lock);
  510. }
  511. spin_unlock_bh(&b_ptr->publ.lock);
  512. read_unlock_bh(&net_lock);
  513. info("Blocked bearer <%s>\n", name);
  514. return TIPC_OK;
  515. }
  516. /**
  517. * bearer_disable -
  518. *
  519. * Note: This routine assumes caller holds net_lock.
  520. */
  521. static int bearer_disable(const char *name)
  522. {
  523. struct bearer *b_ptr;
  524. struct link *l_ptr;
  525. struct link *temp_l_ptr;
  526. if (tipc_mode != TIPC_NET_MODE)
  527. return -ENOPROTOOPT;
  528. b_ptr = bearer_find(name);
  529. if (!b_ptr) {
  530. warn("Attempt to disable unknown bearer <%s>\n", name);
  531. return -EINVAL;
  532. }
  533. disc_stop_link_req(b_ptr->link_req);
  534. spin_lock_bh(&b_ptr->publ.lock);
  535. b_ptr->link_req = NULL;
  536. b_ptr->publ.blocked = 1;
  537. if (b_ptr->media->disable_bearer) {
  538. spin_unlock_bh(&b_ptr->publ.lock);
  539. write_unlock_bh(&net_lock);
  540. b_ptr->media->disable_bearer(&b_ptr->publ);
  541. write_lock_bh(&net_lock);
  542. spin_lock_bh(&b_ptr->publ.lock);
  543. }
  544. list_for_each_entry_safe(l_ptr, temp_l_ptr, &b_ptr->links, link_list) {
  545. link_delete(l_ptr);
  546. }
  547. spin_unlock_bh(&b_ptr->publ.lock);
  548. info("Disabled bearer <%s>\n", name);
  549. memset(b_ptr, 0, sizeof(struct bearer));
  550. return TIPC_OK;
  551. }
  552. int tipc_disable_bearer(const char *name)
  553. {
  554. int res;
  555. write_lock_bh(&net_lock);
  556. res = bearer_disable(name);
  557. write_unlock_bh(&net_lock);
  558. return res;
  559. }
  560. int bearer_init(void)
  561. {
  562. int res;
  563. write_lock_bh(&net_lock);
  564. bearers = kmalloc(MAX_BEARERS * sizeof(struct bearer), GFP_ATOMIC);
  565. media_list = kmalloc(MAX_MEDIA * sizeof(struct media), GFP_ATOMIC);
  566. if (bearers && media_list) {
  567. memset(bearers, 0, MAX_BEARERS * sizeof(struct bearer));
  568. memset(media_list, 0, MAX_MEDIA * sizeof(struct media));
  569. res = TIPC_OK;
  570. } else {
  571. kfree(bearers);
  572. kfree(media_list);
  573. bearers = 0;
  574. media_list = 0;
  575. res = -ENOMEM;
  576. }
  577. write_unlock_bh(&net_lock);
  578. return res;
  579. }
  580. void bearer_stop(void)
  581. {
  582. u32 i;
  583. if (!bearers)
  584. return;
  585. for (i = 0; i < MAX_BEARERS; i++) {
  586. if (bearers[i].active)
  587. bearers[i].publ.blocked = 1;
  588. }
  589. for (i = 0; i < MAX_BEARERS; i++) {
  590. if (bearers[i].active)
  591. bearer_disable(bearers[i].publ.name);
  592. }
  593. kfree(bearers);
  594. kfree(media_list);
  595. bearers = 0;
  596. media_list = 0;
  597. media_count = 0;
  598. }