bcast.c 24 KB

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