bcast.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. /*
  2. * net/tipc/bcast.c: TIPC broadcast code
  3. *
  4. * Copyright (c) 2004-2006, Ericsson AB
  5. * Copyright (c) 2004, Intel Corporation.
  6. * Copyright (c) 2005, 2010-2011, Wind River Systems
  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 "link.h"
  39. #include "port.h"
  40. #include "bcast.h"
  41. #include "name_distr.h"
  42. #define MAX_PKT_DEFAULT_MCAST 1500 /* bcast link max packet size (fixed) */
  43. #define BCLINK_WIN_DEFAULT 20 /* bcast link window size (default) */
  44. /**
  45. * struct tipc_bcbearer_pair - a pair of bearers used by broadcast link
  46. * @primary: pointer to primary bearer
  47. * @secondary: pointer to secondary bearer
  48. *
  49. * Bearers must have same priority and same set of reachable destinations
  50. * to be paired.
  51. */
  52. struct tipc_bcbearer_pair {
  53. struct tipc_bearer *primary;
  54. struct tipc_bearer *secondary;
  55. };
  56. /**
  57. * struct tipc_bcbearer - bearer used by broadcast link
  58. * @bearer: (non-standard) broadcast bearer structure
  59. * @media: (non-standard) broadcast media structure
  60. * @bpairs: array of bearer pairs
  61. * @bpairs_temp: temporary array of bearer pairs used by tipc_bcbearer_sort()
  62. * @remains: temporary node map used by tipc_bcbearer_send()
  63. * @remains_new: temporary node map used tipc_bcbearer_send()
  64. *
  65. * Note: The fields labelled "temporary" are incorporated into the bearer
  66. * to avoid consuming potentially limited stack space through the use of
  67. * large local variables within multicast routines. Concurrent access is
  68. * prevented through use of the spinlock "bc_lock".
  69. */
  70. struct tipc_bcbearer {
  71. struct tipc_bearer bearer;
  72. struct tipc_media media;
  73. struct tipc_bcbearer_pair bpairs[MAX_BEARERS];
  74. struct tipc_bcbearer_pair bpairs_temp[TIPC_MAX_LINK_PRI + 1];
  75. struct tipc_node_map remains;
  76. struct tipc_node_map remains_new;
  77. };
  78. /**
  79. * struct tipc_bclink - link used for broadcast messages
  80. * @link: (non-standard) broadcast link structure
  81. * @node: (non-standard) node structure representing b'cast link's peer node
  82. * @bcast_nodes: map of broadcast-capable nodes
  83. * @retransmit_to: node that most recently requested a retransmit
  84. *
  85. * Handles sequence numbering, fragmentation, bundling, etc.
  86. */
  87. struct tipc_bclink {
  88. struct tipc_link link;
  89. struct tipc_node node;
  90. struct tipc_node_map bcast_nodes;
  91. struct tipc_node *retransmit_to;
  92. };
  93. static struct tipc_bcbearer bcast_bearer;
  94. static struct tipc_bclink bcast_link;
  95. static struct tipc_bcbearer *bcbearer = &bcast_bearer;
  96. static struct tipc_bclink *bclink = &bcast_link;
  97. static struct tipc_link *bcl = &bcast_link.link;
  98. static DEFINE_SPINLOCK(bc_lock);
  99. const char tipc_bclink_name[] = "broadcast-link";
  100. static void tipc_nmap_diff(struct tipc_node_map *nm_a,
  101. struct tipc_node_map *nm_b,
  102. struct tipc_node_map *nm_diff);
  103. static u32 bcbuf_acks(struct sk_buff *buf)
  104. {
  105. return (u32)(unsigned long)TIPC_SKB_CB(buf)->handle;
  106. }
  107. static void bcbuf_set_acks(struct sk_buff *buf, u32 acks)
  108. {
  109. TIPC_SKB_CB(buf)->handle = (void *)(unsigned long)acks;
  110. }
  111. static void bcbuf_decr_acks(struct sk_buff *buf)
  112. {
  113. bcbuf_set_acks(buf, bcbuf_acks(buf) - 1);
  114. }
  115. void tipc_bclink_add_node(u32 addr)
  116. {
  117. spin_lock_bh(&bc_lock);
  118. tipc_nmap_add(&bclink->bcast_nodes, addr);
  119. spin_unlock_bh(&bc_lock);
  120. }
  121. void tipc_bclink_remove_node(u32 addr)
  122. {
  123. spin_lock_bh(&bc_lock);
  124. tipc_nmap_remove(&bclink->bcast_nodes, addr);
  125. spin_unlock_bh(&bc_lock);
  126. }
  127. static void bclink_set_last_sent(void)
  128. {
  129. if (bcl->next_out)
  130. bcl->fsm_msg_cnt = mod(buf_seqno(bcl->next_out) - 1);
  131. else
  132. bcl->fsm_msg_cnt = mod(bcl->next_out_no - 1);
  133. }
  134. u32 tipc_bclink_get_last_sent(void)
  135. {
  136. return bcl->fsm_msg_cnt;
  137. }
  138. static void bclink_update_last_sent(struct tipc_node *node, u32 seqno)
  139. {
  140. node->bclink.last_sent = less_eq(node->bclink.last_sent, seqno) ?
  141. seqno : node->bclink.last_sent;
  142. }
  143. /*
  144. * tipc_bclink_retransmit_to - get most recent node to request retransmission
  145. *
  146. * Called with bc_lock locked
  147. */
  148. struct tipc_node *tipc_bclink_retransmit_to(void)
  149. {
  150. return bclink->retransmit_to;
  151. }
  152. /**
  153. * bclink_retransmit_pkt - retransmit broadcast packets
  154. * @after: sequence number of last packet to *not* retransmit
  155. * @to: sequence number of last packet to retransmit
  156. *
  157. * Called with bc_lock locked
  158. */
  159. static void bclink_retransmit_pkt(u32 after, u32 to)
  160. {
  161. struct sk_buff *buf;
  162. buf = bcl->first_out;
  163. while (buf && less_eq(buf_seqno(buf), after))
  164. buf = buf->next;
  165. tipc_link_retransmit(bcl, buf, mod(to - after));
  166. }
  167. /**
  168. * tipc_bclink_acknowledge - handle acknowledgement of broadcast packets
  169. * @n_ptr: node that sent acknowledgement info
  170. * @acked: broadcast sequence # that has been acknowledged
  171. *
  172. * Node is locked, bc_lock unlocked.
  173. */
  174. void tipc_bclink_acknowledge(struct tipc_node *n_ptr, u32 acked)
  175. {
  176. struct sk_buff *crs;
  177. struct sk_buff *next;
  178. unsigned int released = 0;
  179. spin_lock_bh(&bc_lock);
  180. /* Bail out if tx queue is empty (no clean up is required) */
  181. crs = bcl->first_out;
  182. if (!crs)
  183. goto exit;
  184. /* Determine which messages need to be acknowledged */
  185. if (acked == INVALID_LINK_SEQ) {
  186. /*
  187. * Contact with specified node has been lost, so need to
  188. * acknowledge sent messages only (if other nodes still exist)
  189. * or both sent and unsent messages (otherwise)
  190. */
  191. if (bclink->bcast_nodes.count)
  192. acked = bcl->fsm_msg_cnt;
  193. else
  194. acked = bcl->next_out_no;
  195. } else {
  196. /*
  197. * Bail out if specified sequence number does not correspond
  198. * to a message that has been sent and not yet acknowledged
  199. */
  200. if (less(acked, buf_seqno(crs)) ||
  201. less(bcl->fsm_msg_cnt, acked) ||
  202. less_eq(acked, n_ptr->bclink.acked))
  203. goto exit;
  204. }
  205. /* Skip over packets that node has previously acknowledged */
  206. while (crs && less_eq(buf_seqno(crs), n_ptr->bclink.acked))
  207. crs = crs->next;
  208. /* Update packets that node is now acknowledging */
  209. while (crs && less_eq(buf_seqno(crs), acked)) {
  210. next = crs->next;
  211. if (crs != bcl->next_out)
  212. bcbuf_decr_acks(crs);
  213. else {
  214. bcbuf_set_acks(crs, 0);
  215. bcl->next_out = next;
  216. bclink_set_last_sent();
  217. }
  218. if (bcbuf_acks(crs) == 0) {
  219. bcl->first_out = next;
  220. bcl->out_queue_size--;
  221. buf_discard(crs);
  222. released = 1;
  223. }
  224. crs = next;
  225. }
  226. n_ptr->bclink.acked = acked;
  227. /* Try resolving broadcast link congestion, if necessary */
  228. if (unlikely(bcl->next_out)) {
  229. tipc_link_push_queue(bcl);
  230. bclink_set_last_sent();
  231. }
  232. if (unlikely(released && !list_empty(&bcl->waiting_ports)))
  233. tipc_link_wakeup_ports(bcl, 0);
  234. exit:
  235. spin_unlock_bh(&bc_lock);
  236. }
  237. /*
  238. * tipc_bclink_update_link_state - update broadcast link state
  239. *
  240. * tipc_net_lock and node lock set
  241. */
  242. void tipc_bclink_update_link_state(struct tipc_node *n_ptr, u32 last_sent)
  243. {
  244. struct sk_buff *buf;
  245. /* Ignore "stale" link state info */
  246. if (less_eq(last_sent, n_ptr->bclink.last_in))
  247. return;
  248. /* Update link synchronization state; quit if in sync */
  249. bclink_update_last_sent(n_ptr, last_sent);
  250. if (n_ptr->bclink.last_sent == n_ptr->bclink.last_in)
  251. return;
  252. /* Update out-of-sync state; quit if loss is still unconfirmed */
  253. if ((++n_ptr->bclink.oos_state) == 1) {
  254. if (n_ptr->bclink.deferred_size < (TIPC_MIN_LINK_WIN / 2))
  255. return;
  256. n_ptr->bclink.oos_state++;
  257. }
  258. /* Don't NACK if one has been recently sent (or seen) */
  259. if (n_ptr->bclink.oos_state & 0x1)
  260. return;
  261. /* Send NACK */
  262. buf = tipc_buf_acquire(INT_H_SIZE);
  263. if (buf) {
  264. struct tipc_msg *msg = buf_msg(buf);
  265. tipc_msg_init(msg, BCAST_PROTOCOL, STATE_MSG,
  266. INT_H_SIZE, n_ptr->addr);
  267. msg_set_non_seq(msg, 1);
  268. msg_set_mc_netid(msg, tipc_net_id);
  269. msg_set_bcast_ack(msg, n_ptr->bclink.last_in);
  270. msg_set_bcgap_after(msg, n_ptr->bclink.last_in);
  271. msg_set_bcgap_to(msg, n_ptr->bclink.deferred_head
  272. ? buf_seqno(n_ptr->bclink.deferred_head) - 1
  273. : n_ptr->bclink.last_sent);
  274. spin_lock_bh(&bc_lock);
  275. tipc_bearer_send(&bcbearer->bearer, buf, NULL);
  276. bcl->stats.sent_nacks++;
  277. spin_unlock_bh(&bc_lock);
  278. buf_discard(buf);
  279. n_ptr->bclink.oos_state++;
  280. }
  281. }
  282. /*
  283. * bclink_peek_nack - monitor retransmission requests sent by other nodes
  284. *
  285. * Delay any upcoming NACK by this node if another node has already
  286. * requested the first message this node is going to ask for.
  287. *
  288. * Only tipc_net_lock set.
  289. */
  290. static void bclink_peek_nack(struct tipc_msg *msg)
  291. {
  292. struct tipc_node *n_ptr = tipc_node_find(msg_destnode(msg));
  293. if (unlikely(!n_ptr))
  294. return;
  295. tipc_node_lock(n_ptr);
  296. if (n_ptr->bclink.supported &&
  297. (n_ptr->bclink.last_in != n_ptr->bclink.last_sent) &&
  298. (n_ptr->bclink.last_in == msg_bcgap_after(msg)))
  299. n_ptr->bclink.oos_state = 2;
  300. tipc_node_unlock(n_ptr);
  301. }
  302. /*
  303. * tipc_bclink_send_msg - broadcast a packet to all nodes in cluster
  304. */
  305. int tipc_bclink_send_msg(struct sk_buff *buf)
  306. {
  307. int res;
  308. spin_lock_bh(&bc_lock);
  309. if (!bclink->bcast_nodes.count) {
  310. res = msg_data_sz(buf_msg(buf));
  311. buf_discard(buf);
  312. goto exit;
  313. }
  314. res = tipc_link_send_buf(bcl, buf);
  315. if (likely(res >= 0)) {
  316. bclink_set_last_sent();
  317. bcl->stats.queue_sz_counts++;
  318. bcl->stats.accu_queue_sz += bcl->out_queue_size;
  319. }
  320. exit:
  321. spin_unlock_bh(&bc_lock);
  322. return res;
  323. }
  324. /**
  325. * tipc_bclink_recv_pkt - receive a broadcast packet, and deliver upwards
  326. *
  327. * tipc_net_lock is read_locked, no other locks set
  328. */
  329. void tipc_bclink_recv_pkt(struct sk_buff *buf)
  330. {
  331. struct tipc_msg *msg = buf_msg(buf);
  332. struct tipc_node *node;
  333. u32 next_in;
  334. u32 seqno;
  335. int deferred;
  336. /* Screen out unwanted broadcast messages */
  337. if (msg_mc_netid(msg) != tipc_net_id)
  338. goto exit;
  339. node = tipc_node_find(msg_prevnode(msg));
  340. if (unlikely(!node))
  341. goto exit;
  342. tipc_node_lock(node);
  343. if (unlikely(!node->bclink.supported))
  344. goto unlock;
  345. /* Handle broadcast protocol message */
  346. if (unlikely(msg_user(msg) == BCAST_PROTOCOL)) {
  347. if (msg_type(msg) != STATE_MSG)
  348. goto unlock;
  349. if (msg_destnode(msg) == tipc_own_addr) {
  350. tipc_bclink_acknowledge(node, msg_bcast_ack(msg));
  351. tipc_node_unlock(node);
  352. spin_lock_bh(&bc_lock);
  353. bcl->stats.recv_nacks++;
  354. bclink->retransmit_to = node;
  355. bclink_retransmit_pkt(msg_bcgap_after(msg),
  356. msg_bcgap_to(msg));
  357. spin_unlock_bh(&bc_lock);
  358. } else {
  359. tipc_node_unlock(node);
  360. bclink_peek_nack(msg);
  361. }
  362. goto exit;
  363. }
  364. /* Handle in-sequence broadcast message */
  365. seqno = msg_seqno(msg);
  366. next_in = mod(node->bclink.last_in + 1);
  367. if (likely(seqno == next_in)) {
  368. bclink_update_last_sent(node, seqno);
  369. receive:
  370. node->bclink.last_in = seqno;
  371. node->bclink.oos_state = 0;
  372. spin_lock_bh(&bc_lock);
  373. bcl->stats.recv_info++;
  374. /*
  375. * Unicast an ACK periodically, ensuring that
  376. * all nodes in the cluster don't ACK at the same time
  377. */
  378. if (((seqno - tipc_own_addr) % TIPC_MIN_LINK_WIN) == 0) {
  379. tipc_link_send_proto_msg(
  380. node->active_links[node->addr & 1],
  381. STATE_MSG, 0, 0, 0, 0, 0);
  382. bcl->stats.sent_acks++;
  383. }
  384. /* Deliver message to destination */
  385. if (likely(msg_isdata(msg))) {
  386. spin_unlock_bh(&bc_lock);
  387. tipc_node_unlock(node);
  388. if (likely(msg_mcast(msg)))
  389. tipc_port_recv_mcast(buf, NULL);
  390. else
  391. buf_discard(buf);
  392. } else if (msg_user(msg) == MSG_BUNDLER) {
  393. bcl->stats.recv_bundles++;
  394. bcl->stats.recv_bundled += msg_msgcnt(msg);
  395. spin_unlock_bh(&bc_lock);
  396. tipc_node_unlock(node);
  397. tipc_link_recv_bundle(buf);
  398. } else if (msg_user(msg) == MSG_FRAGMENTER) {
  399. bcl->stats.recv_fragments++;
  400. if (tipc_link_recv_fragment(&node->bclink.defragm,
  401. &buf, &msg))
  402. bcl->stats.recv_fragmented++;
  403. spin_unlock_bh(&bc_lock);
  404. tipc_node_unlock(node);
  405. tipc_net_route_msg(buf);
  406. } else if (msg_user(msg) == NAME_DISTRIBUTOR) {
  407. spin_unlock_bh(&bc_lock);
  408. tipc_node_unlock(node);
  409. tipc_named_recv(buf);
  410. } else {
  411. spin_unlock_bh(&bc_lock);
  412. tipc_node_unlock(node);
  413. buf_discard(buf);
  414. }
  415. buf = NULL;
  416. /* Determine new synchronization state */
  417. tipc_node_lock(node);
  418. if (unlikely(!tipc_node_is_up(node)))
  419. goto unlock;
  420. if (node->bclink.last_in == node->bclink.last_sent)
  421. goto unlock;
  422. if (!node->bclink.deferred_head) {
  423. node->bclink.oos_state = 1;
  424. goto unlock;
  425. }
  426. msg = buf_msg(node->bclink.deferred_head);
  427. seqno = msg_seqno(msg);
  428. next_in = mod(next_in + 1);
  429. if (seqno != next_in)
  430. goto unlock;
  431. /* Take in-sequence message from deferred queue & deliver it */
  432. buf = node->bclink.deferred_head;
  433. node->bclink.deferred_head = buf->next;
  434. node->bclink.deferred_size--;
  435. goto receive;
  436. }
  437. /* Handle out-of-sequence broadcast message */
  438. if (less(next_in, seqno)) {
  439. deferred = tipc_link_defer_pkt(&node->bclink.deferred_head,
  440. &node->bclink.deferred_tail,
  441. buf);
  442. node->bclink.deferred_size += deferred;
  443. bclink_update_last_sent(node, seqno);
  444. buf = NULL;
  445. } else
  446. deferred = 0;
  447. spin_lock_bh(&bc_lock);
  448. if (deferred)
  449. bcl->stats.deferred_recv++;
  450. else
  451. bcl->stats.duplicates++;
  452. spin_unlock_bh(&bc_lock);
  453. unlock:
  454. tipc_node_unlock(node);
  455. exit:
  456. buf_discard(buf);
  457. }
  458. u32 tipc_bclink_acks_missing(struct tipc_node *n_ptr)
  459. {
  460. return (n_ptr->bclink.supported &&
  461. (tipc_bclink_get_last_sent() != n_ptr->bclink.acked));
  462. }
  463. /**
  464. * tipc_bcbearer_send - send a packet through the broadcast pseudo-bearer
  465. *
  466. * Send packet over as many bearers as necessary to reach all nodes
  467. * that have joined the broadcast link.
  468. *
  469. * Returns 0 (packet sent successfully) under all circumstances,
  470. * since the broadcast link's pseudo-bearer never blocks
  471. */
  472. static int tipc_bcbearer_send(struct sk_buff *buf,
  473. struct tipc_bearer *unused1,
  474. struct tipc_media_addr *unused2)
  475. {
  476. int bp_index;
  477. /*
  478. * Prepare broadcast link message for reliable transmission,
  479. * if first time trying to send it;
  480. * preparation is skipped for broadcast link protocol messages
  481. * since they are sent in an unreliable manner and don't need it
  482. */
  483. if (likely(!msg_non_seq(buf_msg(buf)))) {
  484. struct tipc_msg *msg;
  485. bcbuf_set_acks(buf, bclink->bcast_nodes.count);
  486. msg = buf_msg(buf);
  487. msg_set_non_seq(msg, 1);
  488. msg_set_mc_netid(msg, tipc_net_id);
  489. bcl->stats.sent_info++;
  490. if (WARN_ON(!bclink->bcast_nodes.count)) {
  491. dump_stack();
  492. return 0;
  493. }
  494. }
  495. /* Send buffer over bearers until all targets reached */
  496. bcbearer->remains = bclink->bcast_nodes;
  497. for (bp_index = 0; bp_index < MAX_BEARERS; bp_index++) {
  498. struct tipc_bearer *p = bcbearer->bpairs[bp_index].primary;
  499. struct tipc_bearer *s = bcbearer->bpairs[bp_index].secondary;
  500. if (!p)
  501. break; /* no more bearers to try */
  502. tipc_nmap_diff(&bcbearer->remains, &p->nodes, &bcbearer->remains_new);
  503. if (bcbearer->remains_new.count == bcbearer->remains.count)
  504. continue; /* bearer pair doesn't add anything */
  505. if (p->blocked ||
  506. p->media->send_msg(buf, p, &p->media->bcast_addr)) {
  507. /* unable to send on primary bearer */
  508. if (!s || s->blocked ||
  509. s->media->send_msg(buf, s,
  510. &s->media->bcast_addr)) {
  511. /* unable to send on either bearer */
  512. continue;
  513. }
  514. }
  515. if (s) {
  516. bcbearer->bpairs[bp_index].primary = s;
  517. bcbearer->bpairs[bp_index].secondary = p;
  518. }
  519. if (bcbearer->remains_new.count == 0)
  520. break; /* all targets reached */
  521. bcbearer->remains = bcbearer->remains_new;
  522. }
  523. return 0;
  524. }
  525. /**
  526. * tipc_bcbearer_sort - create sets of bearer pairs used by broadcast bearer
  527. */
  528. void tipc_bcbearer_sort(void)
  529. {
  530. struct tipc_bcbearer_pair *bp_temp = bcbearer->bpairs_temp;
  531. struct tipc_bcbearer_pair *bp_curr;
  532. int b_index;
  533. int pri;
  534. spin_lock_bh(&bc_lock);
  535. /* Group bearers by priority (can assume max of two per priority) */
  536. memset(bp_temp, 0, sizeof(bcbearer->bpairs_temp));
  537. for (b_index = 0; b_index < MAX_BEARERS; b_index++) {
  538. struct tipc_bearer *b = &tipc_bearers[b_index];
  539. if (!b->active || !b->nodes.count)
  540. continue;
  541. if (!bp_temp[b->priority].primary)
  542. bp_temp[b->priority].primary = b;
  543. else
  544. bp_temp[b->priority].secondary = b;
  545. }
  546. /* Create array of bearer pairs for broadcasting */
  547. bp_curr = bcbearer->bpairs;
  548. memset(bcbearer->bpairs, 0, sizeof(bcbearer->bpairs));
  549. for (pri = TIPC_MAX_LINK_PRI; pri >= 0; pri--) {
  550. if (!bp_temp[pri].primary)
  551. continue;
  552. bp_curr->primary = bp_temp[pri].primary;
  553. if (bp_temp[pri].secondary) {
  554. if (tipc_nmap_equal(&bp_temp[pri].primary->nodes,
  555. &bp_temp[pri].secondary->nodes)) {
  556. bp_curr->secondary = bp_temp[pri].secondary;
  557. } else {
  558. bp_curr++;
  559. bp_curr->primary = bp_temp[pri].secondary;
  560. }
  561. }
  562. bp_curr++;
  563. }
  564. spin_unlock_bh(&bc_lock);
  565. }
  566. int tipc_bclink_stats(char *buf, const u32 buf_size)
  567. {
  568. struct print_buf pb;
  569. if (!bcl)
  570. return 0;
  571. tipc_printbuf_init(&pb, buf, buf_size);
  572. spin_lock_bh(&bc_lock);
  573. tipc_printf(&pb, "Link <%s>\n"
  574. " Window:%u packets\n",
  575. bcl->name, bcl->queue_limit[0]);
  576. tipc_printf(&pb, " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
  577. bcl->stats.recv_info,
  578. bcl->stats.recv_fragments,
  579. bcl->stats.recv_fragmented,
  580. bcl->stats.recv_bundles,
  581. bcl->stats.recv_bundled);
  582. tipc_printf(&pb, " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
  583. bcl->stats.sent_info,
  584. bcl->stats.sent_fragments,
  585. bcl->stats.sent_fragmented,
  586. bcl->stats.sent_bundles,
  587. bcl->stats.sent_bundled);
  588. tipc_printf(&pb, " RX naks:%u defs:%u dups:%u\n",
  589. bcl->stats.recv_nacks,
  590. bcl->stats.deferred_recv,
  591. bcl->stats.duplicates);
  592. tipc_printf(&pb, " TX naks:%u acks:%u dups:%u\n",
  593. bcl->stats.sent_nacks,
  594. bcl->stats.sent_acks,
  595. bcl->stats.retransmitted);
  596. tipc_printf(&pb, " Congestion bearer:%u link:%u Send queue max:%u avg:%u\n",
  597. bcl->stats.bearer_congs,
  598. bcl->stats.link_congs,
  599. bcl->stats.max_queue_sz,
  600. bcl->stats.queue_sz_counts
  601. ? (bcl->stats.accu_queue_sz / bcl->stats.queue_sz_counts)
  602. : 0);
  603. spin_unlock_bh(&bc_lock);
  604. return tipc_printbuf_validate(&pb);
  605. }
  606. int tipc_bclink_reset_stats(void)
  607. {
  608. if (!bcl)
  609. return -ENOPROTOOPT;
  610. spin_lock_bh(&bc_lock);
  611. memset(&bcl->stats, 0, sizeof(bcl->stats));
  612. spin_unlock_bh(&bc_lock);
  613. return 0;
  614. }
  615. int tipc_bclink_set_queue_limits(u32 limit)
  616. {
  617. if (!bcl)
  618. return -ENOPROTOOPT;
  619. if ((limit < TIPC_MIN_LINK_WIN) || (limit > TIPC_MAX_LINK_WIN))
  620. return -EINVAL;
  621. spin_lock_bh(&bc_lock);
  622. tipc_link_set_queue_limits(bcl, limit);
  623. spin_unlock_bh(&bc_lock);
  624. return 0;
  625. }
  626. void tipc_bclink_init(void)
  627. {
  628. INIT_LIST_HEAD(&bcbearer->bearer.cong_links);
  629. bcbearer->bearer.media = &bcbearer->media;
  630. bcbearer->media.send_msg = tipc_bcbearer_send;
  631. sprintf(bcbearer->media.name, "tipc-broadcast");
  632. INIT_LIST_HEAD(&bcl->waiting_ports);
  633. bcl->next_out_no = 1;
  634. spin_lock_init(&bclink->node.lock);
  635. bcl->owner = &bclink->node;
  636. bcl->max_pkt = MAX_PKT_DEFAULT_MCAST;
  637. tipc_link_set_queue_limits(bcl, BCLINK_WIN_DEFAULT);
  638. bcl->b_ptr = &bcbearer->bearer;
  639. bcl->state = WORKING_WORKING;
  640. strlcpy(bcl->name, tipc_bclink_name, TIPC_MAX_LINK_NAME);
  641. }
  642. void tipc_bclink_stop(void)
  643. {
  644. spin_lock_bh(&bc_lock);
  645. tipc_link_stop(bcl);
  646. spin_unlock_bh(&bc_lock);
  647. memset(bclink, 0, sizeof(*bclink));
  648. memset(bcbearer, 0, sizeof(*bcbearer));
  649. }
  650. /**
  651. * tipc_nmap_add - add a node to a node map
  652. */
  653. void tipc_nmap_add(struct tipc_node_map *nm_ptr, u32 node)
  654. {
  655. int n = tipc_node(node);
  656. int w = n / WSIZE;
  657. u32 mask = (1 << (n % WSIZE));
  658. if ((nm_ptr->map[w] & mask) == 0) {
  659. nm_ptr->count++;
  660. nm_ptr->map[w] |= mask;
  661. }
  662. }
  663. /**
  664. * tipc_nmap_remove - remove a node from a node map
  665. */
  666. void tipc_nmap_remove(struct tipc_node_map *nm_ptr, u32 node)
  667. {
  668. int n = tipc_node(node);
  669. int w = n / WSIZE;
  670. u32 mask = (1 << (n % WSIZE));
  671. if ((nm_ptr->map[w] & mask) != 0) {
  672. nm_ptr->map[w] &= ~mask;
  673. nm_ptr->count--;
  674. }
  675. }
  676. /**
  677. * tipc_nmap_diff - find differences between node maps
  678. * @nm_a: input node map A
  679. * @nm_b: input node map B
  680. * @nm_diff: output node map A-B (i.e. nodes of A that are not in B)
  681. */
  682. static void tipc_nmap_diff(struct tipc_node_map *nm_a,
  683. struct tipc_node_map *nm_b,
  684. struct tipc_node_map *nm_diff)
  685. {
  686. int stop = ARRAY_SIZE(nm_a->map);
  687. int w;
  688. int b;
  689. u32 map;
  690. memset(nm_diff, 0, sizeof(*nm_diff));
  691. for (w = 0; w < stop; w++) {
  692. map = nm_a->map[w] ^ (nm_a->map[w] & nm_b->map[w]);
  693. nm_diff->map[w] = map;
  694. if (map != 0) {
  695. for (b = 0 ; b < WSIZE; b++) {
  696. if (map & (1 << b))
  697. nm_diff->count++;
  698. }
  699. }
  700. }
  701. }
  702. /**
  703. * tipc_port_list_add - add a port to a port list, ensuring no duplicates
  704. */
  705. void tipc_port_list_add(struct tipc_port_list *pl_ptr, u32 port)
  706. {
  707. struct tipc_port_list *item = pl_ptr;
  708. int i;
  709. int item_sz = PLSIZE;
  710. int cnt = pl_ptr->count;
  711. for (; ; cnt -= item_sz, item = item->next) {
  712. if (cnt < PLSIZE)
  713. item_sz = cnt;
  714. for (i = 0; i < item_sz; i++)
  715. if (item->ports[i] == port)
  716. return;
  717. if (i < PLSIZE) {
  718. item->ports[i] = port;
  719. pl_ptr->count++;
  720. return;
  721. }
  722. if (!item->next) {
  723. item->next = kmalloc(sizeof(*item), GFP_ATOMIC);
  724. if (!item->next) {
  725. warn("Incomplete multicast delivery, no memory\n");
  726. return;
  727. }
  728. item->next->next = NULL;
  729. }
  730. }
  731. }
  732. /**
  733. * tipc_port_list_free - free dynamically created entries in port_list chain
  734. *
  735. */
  736. void tipc_port_list_free(struct tipc_port_list *pl_ptr)
  737. {
  738. struct tipc_port_list *item;
  739. struct tipc_port_list *next;
  740. for (item = pl_ptr->next; item; item = next) {
  741. next = item->next;
  742. kfree(item);
  743. }
  744. }