bcast.c 28 KB

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