bcast.c 22 KB

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