bcast.c 23 KB

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