bcast.c 23 KB

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