bcast.c 26 KB

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