bcast.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. /*
  2. * net/tipc/bcast.c: TIPC broadcast code
  3. *
  4. * Copyright (c) 2004-2006, 2014-2015, 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 "socket.h"
  38. #include "msg.h"
  39. #include "bcast.h"
  40. #include "name_distr.h"
  41. #include "core.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. const char tipc_bclink_name[] = "broadcast-link";
  45. static void tipc_nmap_diff(struct tipc_node_map *nm_a,
  46. struct tipc_node_map *nm_b,
  47. struct tipc_node_map *nm_diff);
  48. static void tipc_nmap_add(struct tipc_node_map *nm_ptr, u32 node);
  49. static void tipc_nmap_remove(struct tipc_node_map *nm_ptr, u32 node);
  50. static void tipc_bclink_lock(struct net *net)
  51. {
  52. struct tipc_net *tn = net_generic(net, tipc_net_id);
  53. spin_lock_bh(&tn->bclink->lock);
  54. }
  55. static void tipc_bclink_unlock(struct net *net)
  56. {
  57. struct tipc_net *tn = net_generic(net, tipc_net_id);
  58. spin_unlock_bh(&tn->bclink->lock);
  59. }
  60. void tipc_bclink_input(struct net *net)
  61. {
  62. struct tipc_net *tn = net_generic(net, tipc_net_id);
  63. tipc_sk_mcast_rcv(net, &tn->bclink->arrvq, &tn->bclink->inputq);
  64. }
  65. uint tipc_bclink_get_mtu(void)
  66. {
  67. return MAX_PKT_DEFAULT_MCAST;
  68. }
  69. static u32 bcbuf_acks(struct sk_buff *buf)
  70. {
  71. return (u32)(unsigned long)TIPC_SKB_CB(buf)->handle;
  72. }
  73. static void bcbuf_set_acks(struct sk_buff *buf, u32 acks)
  74. {
  75. TIPC_SKB_CB(buf)->handle = (void *)(unsigned long)acks;
  76. }
  77. static void bcbuf_decr_acks(struct sk_buff *buf)
  78. {
  79. bcbuf_set_acks(buf, bcbuf_acks(buf) - 1);
  80. }
  81. void tipc_bclink_add_node(struct net *net, u32 addr)
  82. {
  83. struct tipc_net *tn = net_generic(net, tipc_net_id);
  84. tipc_bclink_lock(net);
  85. tipc_nmap_add(&tn->bclink->bcast_nodes, addr);
  86. tipc_bclink_unlock(net);
  87. }
  88. void tipc_bclink_remove_node(struct net *net, u32 addr)
  89. {
  90. struct tipc_net *tn = net_generic(net, tipc_net_id);
  91. tipc_bclink_lock(net);
  92. tipc_nmap_remove(&tn->bclink->bcast_nodes, addr);
  93. /* Last node? => reset backlog queue */
  94. if (!tn->bclink->bcast_nodes.count)
  95. tipc_link_purge_backlog(&tn->bclink->link);
  96. tipc_bclink_unlock(net);
  97. }
  98. static void bclink_set_last_sent(struct net *net)
  99. {
  100. struct tipc_net *tn = net_generic(net, tipc_net_id);
  101. struct tipc_link *bcl = tn->bcl;
  102. bcl->silent_intv_cnt = mod(bcl->snd_nxt - 1);
  103. }
  104. u32 tipc_bclink_get_last_sent(struct net *net)
  105. {
  106. struct tipc_net *tn = net_generic(net, tipc_net_id);
  107. return tn->bcl->silent_intv_cnt;
  108. }
  109. static void bclink_update_last_sent(struct tipc_node *node, u32 seqno)
  110. {
  111. node->bclink.last_sent = less_eq(node->bclink.last_sent, seqno) ?
  112. seqno : node->bclink.last_sent;
  113. }
  114. /**
  115. * tipc_bclink_retransmit_to - get most recent node to request retransmission
  116. *
  117. * Called with bclink_lock locked
  118. */
  119. struct tipc_node *tipc_bclink_retransmit_to(struct net *net)
  120. {
  121. struct tipc_net *tn = net_generic(net, tipc_net_id);
  122. return tn->bclink->retransmit_to;
  123. }
  124. /**
  125. * bclink_retransmit_pkt - retransmit broadcast packets
  126. * @after: sequence number of last packet to *not* retransmit
  127. * @to: sequence number of last packet to retransmit
  128. *
  129. * Called with bclink_lock locked
  130. */
  131. static void bclink_retransmit_pkt(struct tipc_net *tn, u32 after, u32 to)
  132. {
  133. struct sk_buff *skb;
  134. struct tipc_link *bcl = tn->bcl;
  135. skb_queue_walk(&bcl->transmq, skb) {
  136. if (more(buf_seqno(skb), after)) {
  137. tipc_link_retransmit(bcl, skb, mod(to - after));
  138. break;
  139. }
  140. }
  141. }
  142. /**
  143. * tipc_bclink_wakeup_users - wake up pending users
  144. *
  145. * Called with no locks taken
  146. */
  147. void tipc_bclink_wakeup_users(struct net *net)
  148. {
  149. struct tipc_net *tn = net_generic(net, tipc_net_id);
  150. tipc_sk_rcv(net, &tn->bclink->link.wakeupq);
  151. }
  152. /**
  153. * tipc_bclink_acknowledge - handle acknowledgement of broadcast packets
  154. * @n_ptr: node that sent acknowledgement info
  155. * @acked: broadcast sequence # that has been acknowledged
  156. *
  157. * Node is locked, bclink_lock unlocked.
  158. */
  159. void tipc_bclink_acknowledge(struct tipc_node *n_ptr, u32 acked)
  160. {
  161. struct sk_buff *skb, *tmp;
  162. unsigned int released = 0;
  163. struct net *net = n_ptr->net;
  164. struct tipc_net *tn = net_generic(net, tipc_net_id);
  165. if (unlikely(!n_ptr->bclink.recv_permitted))
  166. return;
  167. tipc_bclink_lock(net);
  168. /* Bail out if tx queue is empty (no clean up is required) */
  169. skb = skb_peek(&tn->bcl->transmq);
  170. if (!skb)
  171. goto exit;
  172. /* Determine which messages need to be acknowledged */
  173. if (acked == INVALID_LINK_SEQ) {
  174. /*
  175. * Contact with specified node has been lost, so need to
  176. * acknowledge sent messages only (if other nodes still exist)
  177. * or both sent and unsent messages (otherwise)
  178. */
  179. if (tn->bclink->bcast_nodes.count)
  180. acked = tn->bcl->silent_intv_cnt;
  181. else
  182. acked = tn->bcl->snd_nxt;
  183. } else {
  184. /*
  185. * Bail out if specified sequence number does not correspond
  186. * to a message that has been sent and not yet acknowledged
  187. */
  188. if (less(acked, buf_seqno(skb)) ||
  189. less(tn->bcl->silent_intv_cnt, acked) ||
  190. less_eq(acked, n_ptr->bclink.acked))
  191. goto exit;
  192. }
  193. /* Skip over packets that node has previously acknowledged */
  194. skb_queue_walk(&tn->bcl->transmq, skb) {
  195. if (more(buf_seqno(skb), n_ptr->bclink.acked))
  196. break;
  197. }
  198. /* Update packets that node is now acknowledging */
  199. skb_queue_walk_from_safe(&tn->bcl->transmq, skb, tmp) {
  200. if (more(buf_seqno(skb), acked))
  201. break;
  202. bcbuf_decr_acks(skb);
  203. bclink_set_last_sent(net);
  204. if (bcbuf_acks(skb) == 0) {
  205. __skb_unlink(skb, &tn->bcl->transmq);
  206. kfree_skb(skb);
  207. released = 1;
  208. }
  209. }
  210. n_ptr->bclink.acked = acked;
  211. /* Try resolving broadcast link congestion, if necessary */
  212. if (unlikely(skb_peek(&tn->bcl->backlogq))) {
  213. tipc_link_push_packets(tn->bcl);
  214. bclink_set_last_sent(net);
  215. }
  216. if (unlikely(released && !skb_queue_empty(&tn->bcl->wakeupq)))
  217. n_ptr->action_flags |= TIPC_WAKEUP_BCAST_USERS;
  218. exit:
  219. tipc_bclink_unlock(net);
  220. }
  221. /**
  222. * tipc_bclink_update_link_state - update broadcast link state
  223. *
  224. * RCU and node lock set
  225. */
  226. void tipc_bclink_update_link_state(struct tipc_node *n_ptr,
  227. u32 last_sent)
  228. {
  229. struct sk_buff *buf;
  230. struct net *net = n_ptr->net;
  231. struct tipc_net *tn = net_generic(net, tipc_net_id);
  232. /* Ignore "stale" link state info */
  233. if (less_eq(last_sent, n_ptr->bclink.last_in))
  234. return;
  235. /* Update link synchronization state; quit if in sync */
  236. bclink_update_last_sent(n_ptr, last_sent);
  237. if (n_ptr->bclink.last_sent == n_ptr->bclink.last_in)
  238. return;
  239. /* Update out-of-sync state; quit if loss is still unconfirmed */
  240. if ((++n_ptr->bclink.oos_state) == 1) {
  241. if (n_ptr->bclink.deferred_size < (TIPC_MIN_LINK_WIN / 2))
  242. return;
  243. n_ptr->bclink.oos_state++;
  244. }
  245. /* Don't NACK if one has been recently sent (or seen) */
  246. if (n_ptr->bclink.oos_state & 0x1)
  247. return;
  248. /* Send NACK */
  249. buf = tipc_buf_acquire(INT_H_SIZE);
  250. if (buf) {
  251. struct tipc_msg *msg = buf_msg(buf);
  252. struct sk_buff *skb = skb_peek(&n_ptr->bclink.deferdq);
  253. u32 to = skb ? buf_seqno(skb) - 1 : n_ptr->bclink.last_sent;
  254. tipc_msg_init(tn->own_addr, msg, BCAST_PROTOCOL, STATE_MSG,
  255. INT_H_SIZE, n_ptr->addr);
  256. msg_set_non_seq(msg, 1);
  257. msg_set_mc_netid(msg, tn->net_id);
  258. msg_set_bcast_ack(msg, n_ptr->bclink.last_in);
  259. msg_set_bcgap_after(msg, n_ptr->bclink.last_in);
  260. msg_set_bcgap_to(msg, to);
  261. tipc_bclink_lock(net);
  262. tipc_bearer_send(net, MAX_BEARERS, buf, NULL);
  263. tn->bcl->stats.sent_nacks++;
  264. tipc_bclink_unlock(net);
  265. kfree_skb(buf);
  266. n_ptr->bclink.oos_state++;
  267. }
  268. }
  269. /**
  270. * bclink_peek_nack - monitor retransmission requests sent by other nodes
  271. *
  272. * Delay any upcoming NACK by this node if another node has already
  273. * requested the first message this node is going to ask for.
  274. */
  275. static void bclink_peek_nack(struct net *net, struct tipc_msg *msg)
  276. {
  277. struct tipc_node *n_ptr = tipc_node_find(net, msg_destnode(msg));
  278. if (unlikely(!n_ptr))
  279. return;
  280. tipc_node_lock(n_ptr);
  281. if (n_ptr->bclink.recv_permitted &&
  282. (n_ptr->bclink.last_in != n_ptr->bclink.last_sent) &&
  283. (n_ptr->bclink.last_in == msg_bcgap_after(msg)))
  284. n_ptr->bclink.oos_state = 2;
  285. tipc_node_unlock(n_ptr);
  286. tipc_node_put(n_ptr);
  287. }
  288. /* tipc_bclink_xmit - deliver buffer chain to all nodes in cluster
  289. * and to identified node local sockets
  290. * @net: the applicable net namespace
  291. * @list: chain of buffers containing message
  292. * Consumes the buffer chain, except when returning -ELINKCONG
  293. * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
  294. */
  295. int tipc_bclink_xmit(struct net *net, struct sk_buff_head *list)
  296. {
  297. struct tipc_net *tn = net_generic(net, tipc_net_id);
  298. struct tipc_link *bcl = tn->bcl;
  299. struct tipc_bclink *bclink = tn->bclink;
  300. int rc = 0;
  301. int bc = 0;
  302. struct sk_buff *skb;
  303. struct sk_buff_head arrvq;
  304. struct sk_buff_head inputq;
  305. /* Prepare clone of message for local node */
  306. skb = tipc_msg_reassemble(list);
  307. if (unlikely(!skb))
  308. return -EHOSTUNREACH;
  309. /* Broadcast to all nodes */
  310. if (likely(bclink)) {
  311. tipc_bclink_lock(net);
  312. if (likely(bclink->bcast_nodes.count)) {
  313. rc = __tipc_link_xmit(net, bcl, list);
  314. if (likely(!rc)) {
  315. u32 len = skb_queue_len(&bcl->transmq);
  316. bclink_set_last_sent(net);
  317. bcl->stats.queue_sz_counts++;
  318. bcl->stats.accu_queue_sz += len;
  319. }
  320. bc = 1;
  321. }
  322. tipc_bclink_unlock(net);
  323. }
  324. if (unlikely(!bc))
  325. __skb_queue_purge(list);
  326. if (unlikely(rc)) {
  327. kfree_skb(skb);
  328. return rc;
  329. }
  330. /* Deliver message clone */
  331. __skb_queue_head_init(&arrvq);
  332. skb_queue_head_init(&inputq);
  333. __skb_queue_tail(&arrvq, skb);
  334. tipc_sk_mcast_rcv(net, &arrvq, &inputq);
  335. return rc;
  336. }
  337. /**
  338. * bclink_accept_pkt - accept an incoming, in-sequence broadcast packet
  339. *
  340. * Called with both sending node's lock and bclink_lock taken.
  341. */
  342. static void bclink_accept_pkt(struct tipc_node *node, u32 seqno)
  343. {
  344. struct tipc_net *tn = net_generic(node->net, tipc_net_id);
  345. bclink_update_last_sent(node, seqno);
  346. node->bclink.last_in = seqno;
  347. node->bclink.oos_state = 0;
  348. tn->bcl->stats.recv_info++;
  349. /*
  350. * Unicast an ACK periodically, ensuring that
  351. * all nodes in the cluster don't ACK at the same time
  352. */
  353. if (((seqno - tn->own_addr) % TIPC_MIN_LINK_WIN) == 0) {
  354. tipc_link_proto_xmit(node_active_link(node, node->addr),
  355. STATE_MSG, 0, 0, 0, 0);
  356. tn->bcl->stats.sent_acks++;
  357. }
  358. }
  359. /**
  360. * tipc_bclink_rcv - receive a broadcast packet, and deliver upwards
  361. *
  362. * RCU is locked, no other locks set
  363. */
  364. void tipc_bclink_rcv(struct net *net, struct sk_buff *buf)
  365. {
  366. struct tipc_net *tn = net_generic(net, tipc_net_id);
  367. struct tipc_link *bcl = tn->bcl;
  368. struct tipc_msg *msg = buf_msg(buf);
  369. struct tipc_node *node;
  370. u32 next_in;
  371. u32 seqno;
  372. int deferred = 0;
  373. int pos = 0;
  374. struct sk_buff *iskb;
  375. struct sk_buff_head *arrvq, *inputq;
  376. /* Screen out unwanted broadcast messages */
  377. if (msg_mc_netid(msg) != tn->net_id)
  378. goto exit;
  379. node = tipc_node_find(net, msg_prevnode(msg));
  380. if (unlikely(!node))
  381. goto exit;
  382. tipc_node_lock(node);
  383. if (unlikely(!node->bclink.recv_permitted))
  384. goto unlock;
  385. /* Handle broadcast protocol message */
  386. if (unlikely(msg_user(msg) == BCAST_PROTOCOL)) {
  387. if (msg_type(msg) != STATE_MSG)
  388. goto unlock;
  389. if (msg_destnode(msg) == tn->own_addr) {
  390. tipc_bclink_acknowledge(node, msg_bcast_ack(msg));
  391. tipc_bclink_lock(net);
  392. bcl->stats.recv_nacks++;
  393. tn->bclink->retransmit_to = node;
  394. bclink_retransmit_pkt(tn, msg_bcgap_after(msg),
  395. msg_bcgap_to(msg));
  396. tipc_bclink_unlock(net);
  397. tipc_node_unlock(node);
  398. } else {
  399. tipc_node_unlock(node);
  400. bclink_peek_nack(net, msg);
  401. }
  402. tipc_node_put(node);
  403. goto exit;
  404. }
  405. /* Handle in-sequence broadcast message */
  406. seqno = msg_seqno(msg);
  407. next_in = mod(node->bclink.last_in + 1);
  408. arrvq = &tn->bclink->arrvq;
  409. inputq = &tn->bclink->inputq;
  410. if (likely(seqno == next_in)) {
  411. receive:
  412. /* Deliver message to destination */
  413. if (likely(msg_isdata(msg))) {
  414. tipc_bclink_lock(net);
  415. bclink_accept_pkt(node, seqno);
  416. spin_lock_bh(&inputq->lock);
  417. __skb_queue_tail(arrvq, buf);
  418. spin_unlock_bh(&inputq->lock);
  419. node->action_flags |= TIPC_BCAST_MSG_EVT;
  420. tipc_bclink_unlock(net);
  421. tipc_node_unlock(node);
  422. } else if (msg_user(msg) == MSG_BUNDLER) {
  423. tipc_bclink_lock(net);
  424. bclink_accept_pkt(node, seqno);
  425. bcl->stats.recv_bundles++;
  426. bcl->stats.recv_bundled += msg_msgcnt(msg);
  427. pos = 0;
  428. while (tipc_msg_extract(buf, &iskb, &pos)) {
  429. spin_lock_bh(&inputq->lock);
  430. __skb_queue_tail(arrvq, iskb);
  431. spin_unlock_bh(&inputq->lock);
  432. }
  433. node->action_flags |= TIPC_BCAST_MSG_EVT;
  434. tipc_bclink_unlock(net);
  435. tipc_node_unlock(node);
  436. } else if (msg_user(msg) == MSG_FRAGMENTER) {
  437. tipc_bclink_lock(net);
  438. bclink_accept_pkt(node, seqno);
  439. tipc_buf_append(&node->bclink.reasm_buf, &buf);
  440. if (unlikely(!buf && !node->bclink.reasm_buf)) {
  441. tipc_bclink_unlock(net);
  442. goto unlock;
  443. }
  444. bcl->stats.recv_fragments++;
  445. if (buf) {
  446. bcl->stats.recv_fragmented++;
  447. msg = buf_msg(buf);
  448. tipc_bclink_unlock(net);
  449. goto receive;
  450. }
  451. tipc_bclink_unlock(net);
  452. tipc_node_unlock(node);
  453. } else {
  454. tipc_bclink_lock(net);
  455. bclink_accept_pkt(node, seqno);
  456. tipc_bclink_unlock(net);
  457. tipc_node_unlock(node);
  458. kfree_skb(buf);
  459. }
  460. buf = NULL;
  461. /* Determine new synchronization state */
  462. tipc_node_lock(node);
  463. if (unlikely(!tipc_node_is_up(node)))
  464. goto unlock;
  465. if (node->bclink.last_in == node->bclink.last_sent)
  466. goto unlock;
  467. if (skb_queue_empty(&node->bclink.deferdq)) {
  468. node->bclink.oos_state = 1;
  469. goto unlock;
  470. }
  471. msg = buf_msg(skb_peek(&node->bclink.deferdq));
  472. seqno = msg_seqno(msg);
  473. next_in = mod(next_in + 1);
  474. if (seqno != next_in)
  475. goto unlock;
  476. /* Take in-sequence message from deferred queue & deliver it */
  477. buf = __skb_dequeue(&node->bclink.deferdq);
  478. goto receive;
  479. }
  480. /* Handle out-of-sequence broadcast message */
  481. if (less(next_in, seqno)) {
  482. deferred = tipc_link_defer_pkt(&node->bclink.deferdq,
  483. buf);
  484. bclink_update_last_sent(node, seqno);
  485. buf = NULL;
  486. }
  487. tipc_bclink_lock(net);
  488. if (deferred)
  489. bcl->stats.deferred_recv++;
  490. else
  491. bcl->stats.duplicates++;
  492. tipc_bclink_unlock(net);
  493. unlock:
  494. tipc_node_unlock(node);
  495. tipc_node_put(node);
  496. exit:
  497. kfree_skb(buf);
  498. }
  499. u32 tipc_bclink_acks_missing(struct tipc_node *n_ptr)
  500. {
  501. return (n_ptr->bclink.recv_permitted &&
  502. (tipc_bclink_get_last_sent(n_ptr->net) != n_ptr->bclink.acked));
  503. }
  504. /**
  505. * tipc_bcbearer_send - send a packet through the broadcast pseudo-bearer
  506. *
  507. * Send packet over as many bearers as necessary to reach all nodes
  508. * that have joined the broadcast link.
  509. *
  510. * Returns 0 (packet sent successfully) under all circumstances,
  511. * since the broadcast link's pseudo-bearer never blocks
  512. */
  513. static int tipc_bcbearer_send(struct net *net, struct sk_buff *buf,
  514. struct tipc_bearer *unused1,
  515. struct tipc_media_addr *unused2)
  516. {
  517. int bp_index;
  518. struct tipc_msg *msg = buf_msg(buf);
  519. struct tipc_net *tn = net_generic(net, tipc_net_id);
  520. struct tipc_bcbearer *bcbearer = tn->bcbearer;
  521. struct tipc_bclink *bclink = tn->bclink;
  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. bcbuf_set_acks(buf, bclink->bcast_nodes.count);
  529. msg_set_non_seq(msg, 1);
  530. msg_set_mc_netid(msg, tn->net_id);
  531. tn->bcl->stats.sent_info++;
  532. if (WARN_ON(!bclink->bcast_nodes.count)) {
  533. dump_stack();
  534. return 0;
  535. }
  536. }
  537. /* Send buffer over bearers until all targets reached */
  538. bcbearer->remains = bclink->bcast_nodes;
  539. for (bp_index = 0; bp_index < MAX_BEARERS; bp_index++) {
  540. struct tipc_bearer *p = bcbearer->bpairs[bp_index].primary;
  541. struct tipc_bearer *s = bcbearer->bpairs[bp_index].secondary;
  542. struct tipc_bearer *bp[2] = {p, s};
  543. struct tipc_bearer *b = bp[msg_link_selector(msg)];
  544. struct sk_buff *tbuf;
  545. if (!p)
  546. break; /* No more bearers to try */
  547. if (!b)
  548. b = p;
  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(net, 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(net, b->identity, tbuf,
  562. &b->bcast_addr);
  563. kfree_skb(tbuf); /* Bearer keeps a clone */
  564. }
  565. if (bcbearer->remains_new.count == 0)
  566. break; /* All targets reached */
  567. bcbearer->remains = bcbearer->remains_new;
  568. }
  569. return 0;
  570. }
  571. /**
  572. * tipc_bcbearer_sort - create sets of bearer pairs used by broadcast bearer
  573. */
  574. void tipc_bcbearer_sort(struct net *net, struct tipc_node_map *nm_ptr,
  575. u32 node, bool action)
  576. {
  577. struct tipc_net *tn = net_generic(net, tipc_net_id);
  578. struct tipc_bcbearer *bcbearer = tn->bcbearer;
  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(net);
  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(tn->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(net);
  621. }
  622. static int __tipc_nl_add_bc_link_stat(struct sk_buff *skb,
  623. struct tipc_stats *stats)
  624. {
  625. int i;
  626. struct nlattr *nest;
  627. struct nla_map {
  628. __u32 key;
  629. __u32 val;
  630. };
  631. struct nla_map map[] = {
  632. {TIPC_NLA_STATS_RX_INFO, stats->recv_info},
  633. {TIPC_NLA_STATS_RX_FRAGMENTS, stats->recv_fragments},
  634. {TIPC_NLA_STATS_RX_FRAGMENTED, stats->recv_fragmented},
  635. {TIPC_NLA_STATS_RX_BUNDLES, stats->recv_bundles},
  636. {TIPC_NLA_STATS_RX_BUNDLED, stats->recv_bundled},
  637. {TIPC_NLA_STATS_TX_INFO, stats->sent_info},
  638. {TIPC_NLA_STATS_TX_FRAGMENTS, stats->sent_fragments},
  639. {TIPC_NLA_STATS_TX_FRAGMENTED, stats->sent_fragmented},
  640. {TIPC_NLA_STATS_TX_BUNDLES, stats->sent_bundles},
  641. {TIPC_NLA_STATS_TX_BUNDLED, stats->sent_bundled},
  642. {TIPC_NLA_STATS_RX_NACKS, stats->recv_nacks},
  643. {TIPC_NLA_STATS_RX_DEFERRED, stats->deferred_recv},
  644. {TIPC_NLA_STATS_TX_NACKS, stats->sent_nacks},
  645. {TIPC_NLA_STATS_TX_ACKS, stats->sent_acks},
  646. {TIPC_NLA_STATS_RETRANSMITTED, stats->retransmitted},
  647. {TIPC_NLA_STATS_DUPLICATES, stats->duplicates},
  648. {TIPC_NLA_STATS_LINK_CONGS, stats->link_congs},
  649. {TIPC_NLA_STATS_MAX_QUEUE, stats->max_queue_sz},
  650. {TIPC_NLA_STATS_AVG_QUEUE, stats->queue_sz_counts ?
  651. (stats->accu_queue_sz / stats->queue_sz_counts) : 0}
  652. };
  653. nest = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
  654. if (!nest)
  655. return -EMSGSIZE;
  656. for (i = 0; i < ARRAY_SIZE(map); i++)
  657. if (nla_put_u32(skb, map[i].key, map[i].val))
  658. goto msg_full;
  659. nla_nest_end(skb, nest);
  660. return 0;
  661. msg_full:
  662. nla_nest_cancel(skb, nest);
  663. return -EMSGSIZE;
  664. }
  665. int tipc_nl_add_bc_link(struct net *net, struct tipc_nl_msg *msg)
  666. {
  667. int err;
  668. void *hdr;
  669. struct nlattr *attrs;
  670. struct nlattr *prop;
  671. struct tipc_net *tn = net_generic(net, tipc_net_id);
  672. struct tipc_link *bcl = tn->bcl;
  673. if (!bcl)
  674. return 0;
  675. tipc_bclink_lock(net);
  676. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  677. NLM_F_MULTI, TIPC_NL_LINK_GET);
  678. if (!hdr)
  679. return -EMSGSIZE;
  680. attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
  681. if (!attrs)
  682. goto msg_full;
  683. /* The broadcast link is always up */
  684. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
  685. goto attr_msg_full;
  686. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_BROADCAST))
  687. goto attr_msg_full;
  688. if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, bcl->name))
  689. goto attr_msg_full;
  690. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, bcl->rcv_nxt))
  691. goto attr_msg_full;
  692. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, bcl->snd_nxt))
  693. goto attr_msg_full;
  694. prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
  695. if (!prop)
  696. goto attr_msg_full;
  697. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bcl->window))
  698. goto prop_msg_full;
  699. nla_nest_end(msg->skb, prop);
  700. err = __tipc_nl_add_bc_link_stat(msg->skb, &bcl->stats);
  701. if (err)
  702. goto attr_msg_full;
  703. tipc_bclink_unlock(net);
  704. nla_nest_end(msg->skb, attrs);
  705. genlmsg_end(msg->skb, hdr);
  706. return 0;
  707. prop_msg_full:
  708. nla_nest_cancel(msg->skb, prop);
  709. attr_msg_full:
  710. nla_nest_cancel(msg->skb, attrs);
  711. msg_full:
  712. tipc_bclink_unlock(net);
  713. genlmsg_cancel(msg->skb, hdr);
  714. return -EMSGSIZE;
  715. }
  716. int tipc_bclink_reset_stats(struct net *net)
  717. {
  718. struct tipc_net *tn = net_generic(net, tipc_net_id);
  719. struct tipc_link *bcl = tn->bcl;
  720. if (!bcl)
  721. return -ENOPROTOOPT;
  722. tipc_bclink_lock(net);
  723. memset(&bcl->stats, 0, sizeof(bcl->stats));
  724. tipc_bclink_unlock(net);
  725. return 0;
  726. }
  727. int tipc_bclink_set_queue_limits(struct net *net, u32 limit)
  728. {
  729. struct tipc_net *tn = net_generic(net, tipc_net_id);
  730. struct tipc_link *bcl = tn->bcl;
  731. if (!bcl)
  732. return -ENOPROTOOPT;
  733. if ((limit < TIPC_MIN_LINK_WIN) || (limit > TIPC_MAX_LINK_WIN))
  734. return -EINVAL;
  735. tipc_bclink_lock(net);
  736. tipc_link_set_queue_limits(bcl, limit);
  737. tipc_bclink_unlock(net);
  738. return 0;
  739. }
  740. int tipc_nl_bc_link_set(struct net *net, struct nlattr *attrs[])
  741. {
  742. int err;
  743. u32 win;
  744. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  745. if (!attrs[TIPC_NLA_LINK_PROP])
  746. return -EINVAL;
  747. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP], props);
  748. if (err)
  749. return err;
  750. if (!props[TIPC_NLA_PROP_WIN])
  751. return -EOPNOTSUPP;
  752. win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  753. return tipc_bclink_set_queue_limits(net, win);
  754. }
  755. int tipc_bclink_init(struct net *net)
  756. {
  757. struct tipc_net *tn = net_generic(net, tipc_net_id);
  758. struct tipc_bcbearer *bcbearer;
  759. struct tipc_bclink *bclink;
  760. struct tipc_link *bcl;
  761. bcbearer = kzalloc(sizeof(*bcbearer), GFP_ATOMIC);
  762. if (!bcbearer)
  763. return -ENOMEM;
  764. bclink = kzalloc(sizeof(*bclink), GFP_ATOMIC);
  765. if (!bclink) {
  766. kfree(bcbearer);
  767. return -ENOMEM;
  768. }
  769. bcl = &bclink->link;
  770. bcbearer->bearer.media = &bcbearer->media;
  771. bcbearer->media.send_msg = tipc_bcbearer_send;
  772. sprintf(bcbearer->media.name, "tipc-broadcast");
  773. spin_lock_init(&bclink->lock);
  774. __skb_queue_head_init(&bcl->transmq);
  775. __skb_queue_head_init(&bcl->backlogq);
  776. __skb_queue_head_init(&bcl->deferdq);
  777. skb_queue_head_init(&bcl->wakeupq);
  778. bcl->snd_nxt = 1;
  779. spin_lock_init(&bclink->node.lock);
  780. __skb_queue_head_init(&bclink->arrvq);
  781. skb_queue_head_init(&bclink->inputq);
  782. bcl->owner = &bclink->node;
  783. bcl->owner->net = net;
  784. bcl->mtu = MAX_PKT_DEFAULT_MCAST;
  785. tipc_link_set_queue_limits(bcl, BCLINK_WIN_DEFAULT);
  786. bcl->bearer_id = MAX_BEARERS;
  787. rcu_assign_pointer(tn->bearer_list[MAX_BEARERS], &bcbearer->bearer);
  788. bcl->state = WORKING_WORKING;
  789. bcl->pmsg = (struct tipc_msg *)&bcl->proto_msg;
  790. msg_set_prevnode(bcl->pmsg, tn->own_addr);
  791. strlcpy(bcl->name, tipc_bclink_name, TIPC_MAX_LINK_NAME);
  792. tn->bcbearer = bcbearer;
  793. tn->bclink = bclink;
  794. tn->bcl = bcl;
  795. return 0;
  796. }
  797. void tipc_bclink_stop(struct net *net)
  798. {
  799. struct tipc_net *tn = net_generic(net, tipc_net_id);
  800. tipc_bclink_lock(net);
  801. tipc_link_purge_queues(tn->bcl);
  802. tipc_bclink_unlock(net);
  803. RCU_INIT_POINTER(tn->bearer_list[BCBEARER], NULL);
  804. synchronize_net();
  805. kfree(tn->bcbearer);
  806. kfree(tn->bclink);
  807. }
  808. /**
  809. * tipc_nmap_add - add a node to a node map
  810. */
  811. static void tipc_nmap_add(struct tipc_node_map *nm_ptr, u32 node)
  812. {
  813. int n = tipc_node(node);
  814. int w = n / WSIZE;
  815. u32 mask = (1 << (n % WSIZE));
  816. if ((nm_ptr->map[w] & mask) == 0) {
  817. nm_ptr->count++;
  818. nm_ptr->map[w] |= mask;
  819. }
  820. }
  821. /**
  822. * tipc_nmap_remove - remove a node from a node map
  823. */
  824. static void tipc_nmap_remove(struct tipc_node_map *nm_ptr, u32 node)
  825. {
  826. int n = tipc_node(node);
  827. int w = n / WSIZE;
  828. u32 mask = (1 << (n % WSIZE));
  829. if ((nm_ptr->map[w] & mask) != 0) {
  830. nm_ptr->map[w] &= ~mask;
  831. nm_ptr->count--;
  832. }
  833. }
  834. /**
  835. * tipc_nmap_diff - find differences between node maps
  836. * @nm_a: input node map A
  837. * @nm_b: input node map B
  838. * @nm_diff: output node map A-B (i.e. nodes of A that are not in B)
  839. */
  840. static void tipc_nmap_diff(struct tipc_node_map *nm_a,
  841. struct tipc_node_map *nm_b,
  842. struct tipc_node_map *nm_diff)
  843. {
  844. int stop = ARRAY_SIZE(nm_a->map);
  845. int w;
  846. int b;
  847. u32 map;
  848. memset(nm_diff, 0, sizeof(*nm_diff));
  849. for (w = 0; w < stop; w++) {
  850. map = nm_a->map[w] ^ (nm_a->map[w] & nm_b->map[w]);
  851. nm_diff->map[w] = map;
  852. if (map != 0) {
  853. for (b = 0 ; b < WSIZE; b++) {
  854. if (map & (1 << b))
  855. nm_diff->count++;
  856. }
  857. }
  858. }
  859. }