bcast.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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. node->bclink.deferred_size--;
  481. goto receive;
  482. }
  483. /* Handle out-of-sequence broadcast message */
  484. if (less(next_in, seqno)) {
  485. deferred = tipc_link_defer_pkt(&node->bclink.deferred_head,
  486. &node->bclink.deferred_tail,
  487. buf);
  488. node->bclink.deferred_size += deferred;
  489. bclink_update_last_sent(node, seqno);
  490. buf = NULL;
  491. } else
  492. deferred = 0;
  493. tipc_bclink_lock();
  494. if (deferred)
  495. bcl->stats.deferred_recv++;
  496. else
  497. bcl->stats.duplicates++;
  498. tipc_bclink_unlock();
  499. unlock:
  500. tipc_node_unlock(node);
  501. exit:
  502. kfree_skb(buf);
  503. }
  504. u32 tipc_bclink_acks_missing(struct tipc_node *n_ptr)
  505. {
  506. return (n_ptr->bclink.recv_permitted &&
  507. (tipc_bclink_get_last_sent() != n_ptr->bclink.acked));
  508. }
  509. /**
  510. * tipc_bcbearer_send - send a packet through the broadcast pseudo-bearer
  511. *
  512. * Send packet over as many bearers as necessary to reach all nodes
  513. * that have joined the broadcast link.
  514. *
  515. * Returns 0 (packet sent successfully) under all circumstances,
  516. * since the broadcast link's pseudo-bearer never blocks
  517. */
  518. static int tipc_bcbearer_send(struct sk_buff *buf, struct tipc_bearer *unused1,
  519. struct tipc_media_addr *unused2)
  520. {
  521. int bp_index;
  522. /* Prepare broadcast link message for reliable transmission,
  523. * if first time trying to send it;
  524. * preparation is skipped for broadcast link protocol messages
  525. * since they are sent in an unreliable manner and don't need it
  526. */
  527. if (likely(!msg_non_seq(buf_msg(buf)))) {
  528. struct tipc_msg *msg;
  529. bcbuf_set_acks(buf, bclink->bcast_nodes.count);
  530. msg = buf_msg(buf);
  531. msg_set_non_seq(msg, 1);
  532. msg_set_mc_netid(msg, tipc_net_id);
  533. bcl->stats.sent_info++;
  534. if (WARN_ON(!bclink->bcast_nodes.count)) {
  535. dump_stack();
  536. return 0;
  537. }
  538. }
  539. /* Send buffer over bearers until all targets reached */
  540. bcbearer->remains = bclink->bcast_nodes;
  541. for (bp_index = 0; bp_index < MAX_BEARERS; bp_index++) {
  542. struct tipc_bearer *p = bcbearer->bpairs[bp_index].primary;
  543. struct tipc_bearer *s = bcbearer->bpairs[bp_index].secondary;
  544. struct tipc_bearer *b = p;
  545. struct sk_buff *tbuf;
  546. if (!p)
  547. break; /* No more bearers to try */
  548. tipc_nmap_diff(&bcbearer->remains, &b->nodes,
  549. &bcbearer->remains_new);
  550. if (bcbearer->remains_new.count == bcbearer->remains.count)
  551. continue; /* Nothing added by bearer pair */
  552. if (bp_index == 0) {
  553. /* Use original buffer for first bearer */
  554. tipc_bearer_send(b->identity, buf, &b->bcast_addr);
  555. } else {
  556. /* Avoid concurrent buffer access */
  557. tbuf = pskb_copy(buf, GFP_ATOMIC);
  558. if (!tbuf)
  559. break;
  560. tipc_bearer_send(b->identity, tbuf, &b->bcast_addr);
  561. kfree_skb(tbuf); /* Bearer keeps a clone */
  562. }
  563. /* Swap bearers for next packet */
  564. if (s) {
  565. bcbearer->bpairs[bp_index].primary = s;
  566. bcbearer->bpairs[bp_index].secondary = p;
  567. }
  568. if (bcbearer->remains_new.count == 0)
  569. break; /* All targets reached */
  570. bcbearer->remains = bcbearer->remains_new;
  571. }
  572. return 0;
  573. }
  574. /**
  575. * tipc_bcbearer_sort - create sets of bearer pairs used by broadcast bearer
  576. */
  577. void tipc_bcbearer_sort(struct tipc_node_map *nm_ptr, u32 node, bool action)
  578. {
  579. struct tipc_bcbearer_pair *bp_temp = bcbearer->bpairs_temp;
  580. struct tipc_bcbearer_pair *bp_curr;
  581. struct tipc_bearer *b;
  582. int b_index;
  583. int pri;
  584. tipc_bclink_lock();
  585. if (action)
  586. tipc_nmap_add(nm_ptr, node);
  587. else
  588. tipc_nmap_remove(nm_ptr, node);
  589. /* Group bearers by priority (can assume max of two per priority) */
  590. memset(bp_temp, 0, sizeof(bcbearer->bpairs_temp));
  591. rcu_read_lock();
  592. for (b_index = 0; b_index < MAX_BEARERS; b_index++) {
  593. b = rcu_dereference_rtnl(bearer_list[b_index]);
  594. if (!b || !b->nodes.count)
  595. continue;
  596. if (!bp_temp[b->priority].primary)
  597. bp_temp[b->priority].primary = b;
  598. else
  599. bp_temp[b->priority].secondary = b;
  600. }
  601. rcu_read_unlock();
  602. /* Create array of bearer pairs for broadcasting */
  603. bp_curr = bcbearer->bpairs;
  604. memset(bcbearer->bpairs, 0, sizeof(bcbearer->bpairs));
  605. for (pri = TIPC_MAX_LINK_PRI; pri >= 0; pri--) {
  606. if (!bp_temp[pri].primary)
  607. continue;
  608. bp_curr->primary = bp_temp[pri].primary;
  609. if (bp_temp[pri].secondary) {
  610. if (tipc_nmap_equal(&bp_temp[pri].primary->nodes,
  611. &bp_temp[pri].secondary->nodes)) {
  612. bp_curr->secondary = bp_temp[pri].secondary;
  613. } else {
  614. bp_curr++;
  615. bp_curr->primary = bp_temp[pri].secondary;
  616. }
  617. }
  618. bp_curr++;
  619. }
  620. tipc_bclink_unlock();
  621. }
  622. int tipc_bclink_stats(char *buf, const u32 buf_size)
  623. {
  624. int ret;
  625. struct tipc_stats *s;
  626. if (!bcl)
  627. return 0;
  628. tipc_bclink_lock();
  629. s = &bcl->stats;
  630. ret = tipc_snprintf(buf, buf_size, "Link <%s>\n"
  631. " Window:%u packets\n",
  632. bcl->name, bcl->queue_limit[0]);
  633. ret += tipc_snprintf(buf + ret, buf_size - ret,
  634. " RX packets:%u fragments:%u/%u bundles:%u/%u\n",
  635. s->recv_info, s->recv_fragments,
  636. s->recv_fragmented, s->recv_bundles,
  637. s->recv_bundled);
  638. ret += tipc_snprintf(buf + ret, buf_size - ret,
  639. " TX packets:%u fragments:%u/%u bundles:%u/%u\n",
  640. s->sent_info, s->sent_fragments,
  641. s->sent_fragmented, s->sent_bundles,
  642. s->sent_bundled);
  643. ret += tipc_snprintf(buf + ret, buf_size - ret,
  644. " RX naks:%u defs:%u dups:%u\n",
  645. s->recv_nacks, s->deferred_recv, s->duplicates);
  646. ret += tipc_snprintf(buf + ret, buf_size - ret,
  647. " TX naks:%u acks:%u dups:%u\n",
  648. s->sent_nacks, s->sent_acks, s->retransmitted);
  649. ret += tipc_snprintf(buf + ret, buf_size - ret,
  650. " Congestion link:%u Send queue max:%u avg:%u\n",
  651. s->link_congs, s->max_queue_sz,
  652. s->queue_sz_counts ?
  653. (s->accu_queue_sz / s->queue_sz_counts) : 0);
  654. tipc_bclink_unlock();
  655. return ret;
  656. }
  657. int tipc_bclink_reset_stats(void)
  658. {
  659. if (!bcl)
  660. return -ENOPROTOOPT;
  661. tipc_bclink_lock();
  662. memset(&bcl->stats, 0, sizeof(bcl->stats));
  663. tipc_bclink_unlock();
  664. return 0;
  665. }
  666. int tipc_bclink_set_queue_limits(u32 limit)
  667. {
  668. if (!bcl)
  669. return -ENOPROTOOPT;
  670. if ((limit < TIPC_MIN_LINK_WIN) || (limit > TIPC_MAX_LINK_WIN))
  671. return -EINVAL;
  672. tipc_bclink_lock();
  673. tipc_link_set_queue_limits(bcl, limit);
  674. tipc_bclink_unlock();
  675. return 0;
  676. }
  677. int tipc_bclink_init(void)
  678. {
  679. bcbearer = kzalloc(sizeof(*bcbearer), GFP_ATOMIC);
  680. if (!bcbearer)
  681. return -ENOMEM;
  682. bclink = kzalloc(sizeof(*bclink), GFP_ATOMIC);
  683. if (!bclink) {
  684. kfree(bcbearer);
  685. return -ENOMEM;
  686. }
  687. bcl = &bclink->link;
  688. bcbearer->bearer.media = &bcbearer->media;
  689. bcbearer->media.send_msg = tipc_bcbearer_send;
  690. sprintf(bcbearer->media.name, "tipc-broadcast");
  691. spin_lock_init(&bclink->lock);
  692. INIT_LIST_HEAD(&bcl->waiting_ports);
  693. bcl->next_out_no = 1;
  694. spin_lock_init(&bclink->node.lock);
  695. bcl->owner = &bclink->node;
  696. bcl->max_pkt = MAX_PKT_DEFAULT_MCAST;
  697. tipc_link_set_queue_limits(bcl, BCLINK_WIN_DEFAULT);
  698. bcl->bearer_id = MAX_BEARERS;
  699. rcu_assign_pointer(bearer_list[MAX_BEARERS], &bcbearer->bearer);
  700. bcl->state = WORKING_WORKING;
  701. strlcpy(bcl->name, tipc_bclink_name, TIPC_MAX_LINK_NAME);
  702. return 0;
  703. }
  704. void tipc_bclink_stop(void)
  705. {
  706. tipc_bclink_lock();
  707. tipc_link_purge_queues(bcl);
  708. tipc_bclink_unlock();
  709. RCU_INIT_POINTER(bearer_list[BCBEARER], NULL);
  710. synchronize_net();
  711. kfree(bcbearer);
  712. kfree(bclink);
  713. }
  714. /**
  715. * tipc_nmap_add - add a node to a node map
  716. */
  717. static void tipc_nmap_add(struct tipc_node_map *nm_ptr, u32 node)
  718. {
  719. int n = tipc_node(node);
  720. int w = n / WSIZE;
  721. u32 mask = (1 << (n % WSIZE));
  722. if ((nm_ptr->map[w] & mask) == 0) {
  723. nm_ptr->count++;
  724. nm_ptr->map[w] |= mask;
  725. }
  726. }
  727. /**
  728. * tipc_nmap_remove - remove a node from a node map
  729. */
  730. static void tipc_nmap_remove(struct tipc_node_map *nm_ptr, u32 node)
  731. {
  732. int n = tipc_node(node);
  733. int w = n / WSIZE;
  734. u32 mask = (1 << (n % WSIZE));
  735. if ((nm_ptr->map[w] & mask) != 0) {
  736. nm_ptr->map[w] &= ~mask;
  737. nm_ptr->count--;
  738. }
  739. }
  740. /**
  741. * tipc_nmap_diff - find differences between node maps
  742. * @nm_a: input node map A
  743. * @nm_b: input node map B
  744. * @nm_diff: output node map A-B (i.e. nodes of A that are not in B)
  745. */
  746. static void tipc_nmap_diff(struct tipc_node_map *nm_a,
  747. struct tipc_node_map *nm_b,
  748. struct tipc_node_map *nm_diff)
  749. {
  750. int stop = ARRAY_SIZE(nm_a->map);
  751. int w;
  752. int b;
  753. u32 map;
  754. memset(nm_diff, 0, sizeof(*nm_diff));
  755. for (w = 0; w < stop; w++) {
  756. map = nm_a->map[w] ^ (nm_a->map[w] & nm_b->map[w]);
  757. nm_diff->map[w] = map;
  758. if (map != 0) {
  759. for (b = 0 ; b < WSIZE; b++) {
  760. if (map & (1 << b))
  761. nm_diff->count++;
  762. }
  763. }
  764. }
  765. }
  766. /**
  767. * tipc_port_list_add - add a port to a port list, ensuring no duplicates
  768. */
  769. void tipc_port_list_add(struct tipc_port_list *pl_ptr, u32 port)
  770. {
  771. struct tipc_port_list *item = pl_ptr;
  772. int i;
  773. int item_sz = PLSIZE;
  774. int cnt = pl_ptr->count;
  775. for (; ; cnt -= item_sz, item = item->next) {
  776. if (cnt < PLSIZE)
  777. item_sz = cnt;
  778. for (i = 0; i < item_sz; i++)
  779. if (item->ports[i] == port)
  780. return;
  781. if (i < PLSIZE) {
  782. item->ports[i] = port;
  783. pl_ptr->count++;
  784. return;
  785. }
  786. if (!item->next) {
  787. item->next = kmalloc(sizeof(*item), GFP_ATOMIC);
  788. if (!item->next) {
  789. pr_warn("Incomplete multicast delivery, no memory\n");
  790. return;
  791. }
  792. item->next->next = NULL;
  793. }
  794. }
  795. }
  796. /**
  797. * tipc_port_list_free - free dynamically created entries in port_list chain
  798. *
  799. */
  800. void tipc_port_list_free(struct tipc_port_list *pl_ptr)
  801. {
  802. struct tipc_port_list *item;
  803. struct tipc_port_list *next;
  804. for (item = pl_ptr->next; item; item = next) {
  805. next = item->next;
  806. kfree(item);
  807. }
  808. }