bcast.c 24 KB

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