bcast.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 <linux/tipc_config.h>
  38. #include "socket.h"
  39. #include "msg.h"
  40. #include "bcast.h"
  41. #include "name_distr.h"
  42. #include "link.h"
  43. #include "node.h"
  44. #define BCLINK_WIN_DEFAULT 50 /* bcast link window size (default) */
  45. #define BCLINK_WIN_MIN 32 /* bcast minimum link window size */
  46. const char tipc_bclink_name[] = "broadcast-link";
  47. /**
  48. * struct tipc_bc_base - base structure for keeping broadcast send state
  49. * @link: broadcast send link structure
  50. * @inputq: data input queue; will only carry SOCK_WAKEUP messages
  51. * @dest: array keeping number of reachable destinations per bearer
  52. * @primary_bearer: a bearer having links to all broadcast destinations, if any
  53. */
  54. struct tipc_bc_base {
  55. struct tipc_link *link;
  56. struct sk_buff_head inputq;
  57. int dests[MAX_BEARERS];
  58. int primary_bearer;
  59. };
  60. static struct tipc_bc_base *tipc_bc_base(struct net *net)
  61. {
  62. return tipc_net(net)->bcbase;
  63. }
  64. int tipc_bcast_get_mtu(struct net *net)
  65. {
  66. return tipc_link_mtu(tipc_bc_sndlink(net));
  67. }
  68. /* tipc_bcbase_select_primary(): find a bearer with links to all destinations,
  69. * if any, and make it primary bearer
  70. */
  71. static void tipc_bcbase_select_primary(struct net *net)
  72. {
  73. struct tipc_bc_base *bb = tipc_bc_base(net);
  74. int all_dests = tipc_link_bc_peers(bb->link);
  75. int i, mtu;
  76. bb->primary_bearer = INVALID_BEARER_ID;
  77. if (!all_dests)
  78. return;
  79. for (i = 0; i < MAX_BEARERS; i++) {
  80. if (!bb->dests[i])
  81. continue;
  82. mtu = tipc_bearer_mtu(net, i);
  83. if (mtu < tipc_link_mtu(bb->link))
  84. tipc_link_set_mtu(bb->link, mtu);
  85. if (bb->dests[i] < all_dests)
  86. continue;
  87. bb->primary_bearer = i;
  88. /* Reduce risk that all nodes select same primary */
  89. if ((i ^ tipc_own_addr(net)) & 1)
  90. break;
  91. }
  92. }
  93. void tipc_bcast_inc_bearer_dst_cnt(struct net *net, int bearer_id)
  94. {
  95. struct tipc_bc_base *bb = tipc_bc_base(net);
  96. tipc_bcast_lock(net);
  97. bb->dests[bearer_id]++;
  98. tipc_bcbase_select_primary(net);
  99. tipc_bcast_unlock(net);
  100. }
  101. void tipc_bcast_dec_bearer_dst_cnt(struct net *net, int bearer_id)
  102. {
  103. struct tipc_bc_base *bb = tipc_bc_base(net);
  104. tipc_bcast_lock(net);
  105. bb->dests[bearer_id]--;
  106. tipc_bcbase_select_primary(net);
  107. tipc_bcast_unlock(net);
  108. }
  109. /* tipc_bcbase_xmit - broadcast a packet queue across one or more bearers
  110. *
  111. * Note that number of reachable destinations, as indicated in the dests[]
  112. * array, may transitionally differ from the number of destinations indicated
  113. * in each sent buffer. We can sustain this. Excess destination nodes will
  114. * drop and never acknowledge the unexpected packets, and missing destinations
  115. * will either require retransmission (if they are just about to be added to
  116. * the bearer), or be removed from the buffer's 'ackers' counter (if they
  117. * just went down)
  118. */
  119. static void tipc_bcbase_xmit(struct net *net, struct sk_buff_head *xmitq)
  120. {
  121. int bearer_id;
  122. struct tipc_bc_base *bb = tipc_bc_base(net);
  123. struct sk_buff *skb, *_skb;
  124. struct sk_buff_head _xmitq;
  125. if (skb_queue_empty(xmitq))
  126. return;
  127. /* The typical case: at least one bearer has links to all nodes */
  128. bearer_id = bb->primary_bearer;
  129. if (bearer_id >= 0) {
  130. tipc_bearer_bc_xmit(net, bearer_id, xmitq);
  131. return;
  132. }
  133. /* We have to transmit across all bearers */
  134. skb_queue_head_init(&_xmitq);
  135. for (bearer_id = 0; bearer_id < MAX_BEARERS; bearer_id++) {
  136. if (!bb->dests[bearer_id])
  137. continue;
  138. skb_queue_walk(xmitq, skb) {
  139. _skb = pskb_copy_for_clone(skb, GFP_ATOMIC);
  140. if (!_skb)
  141. break;
  142. __skb_queue_tail(&_xmitq, _skb);
  143. }
  144. tipc_bearer_bc_xmit(net, bearer_id, &_xmitq);
  145. }
  146. __skb_queue_purge(xmitq);
  147. __skb_queue_purge(&_xmitq);
  148. }
  149. /* tipc_bcast_xmit - deliver buffer chain to all nodes in cluster
  150. * and to identified node local sockets
  151. * @net: the applicable net namespace
  152. * @list: chain of buffers containing message
  153. * Consumes the buffer chain, except when returning -ELINKCONG
  154. * Returns 0 if success, otherwise errno: -ELINKCONG,-EHOSTUNREACH,-EMSGSIZE
  155. */
  156. int tipc_bcast_xmit(struct net *net, struct sk_buff_head *list)
  157. {
  158. struct tipc_link *l = tipc_bc_sndlink(net);
  159. struct sk_buff_head xmitq, inputq, rcvq;
  160. int rc = 0;
  161. __skb_queue_head_init(&rcvq);
  162. __skb_queue_head_init(&xmitq);
  163. skb_queue_head_init(&inputq);
  164. /* Prepare message clone for local node */
  165. if (unlikely(!tipc_msg_reassemble(list, &rcvq)))
  166. return -EHOSTUNREACH;
  167. tipc_bcast_lock(net);
  168. if (tipc_link_bc_peers(l))
  169. rc = tipc_link_xmit(l, list, &xmitq);
  170. tipc_bcast_unlock(net);
  171. /* Don't send to local node if adding to link failed */
  172. if (unlikely(rc)) {
  173. __skb_queue_purge(&rcvq);
  174. return rc;
  175. }
  176. /* Broadcast to all nodes, inluding local node */
  177. tipc_bcbase_xmit(net, &xmitq);
  178. tipc_sk_mcast_rcv(net, &rcvq, &inputq);
  179. __skb_queue_purge(list);
  180. return 0;
  181. }
  182. /* tipc_bcast_rcv - receive a broadcast packet, and deliver to rcv link
  183. *
  184. * RCU is locked, no other locks set
  185. */
  186. int tipc_bcast_rcv(struct net *net, struct tipc_link *l, struct sk_buff *skb)
  187. {
  188. struct tipc_msg *hdr = buf_msg(skb);
  189. struct sk_buff_head *inputq = &tipc_bc_base(net)->inputq;
  190. struct sk_buff_head xmitq;
  191. int rc;
  192. __skb_queue_head_init(&xmitq);
  193. if (msg_mc_netid(hdr) != tipc_netid(net) || !tipc_link_is_up(l)) {
  194. kfree_skb(skb);
  195. return 0;
  196. }
  197. tipc_bcast_lock(net);
  198. if (msg_user(hdr) == BCAST_PROTOCOL)
  199. rc = tipc_link_bc_nack_rcv(l, skb, &xmitq);
  200. else
  201. rc = tipc_link_rcv(l, skb, NULL);
  202. tipc_bcast_unlock(net);
  203. tipc_bcbase_xmit(net, &xmitq);
  204. /* Any socket wakeup messages ? */
  205. if (!skb_queue_empty(inputq))
  206. tipc_sk_rcv(net, inputq);
  207. return rc;
  208. }
  209. /* tipc_bcast_ack_rcv - receive and handle a broadcast acknowledge
  210. *
  211. * RCU is locked, no other locks set
  212. */
  213. void tipc_bcast_ack_rcv(struct net *net, struct tipc_link *l,
  214. struct tipc_msg *hdr)
  215. {
  216. struct sk_buff_head *inputq = &tipc_bc_base(net)->inputq;
  217. u16 acked = msg_bcast_ack(hdr);
  218. struct sk_buff_head xmitq;
  219. /* Ignore bc acks sent by peer before bcast synch point was received */
  220. if (msg_bc_ack_invalid(hdr))
  221. return;
  222. __skb_queue_head_init(&xmitq);
  223. tipc_bcast_lock(net);
  224. tipc_link_bc_ack_rcv(l, acked, &xmitq);
  225. tipc_bcast_unlock(net);
  226. tipc_bcbase_xmit(net, &xmitq);
  227. /* Any socket wakeup messages ? */
  228. if (!skb_queue_empty(inputq))
  229. tipc_sk_rcv(net, inputq);
  230. }
  231. /* tipc_bcast_synch_rcv - check and update rcv link with peer's send state
  232. *
  233. * RCU is locked, no other locks set
  234. */
  235. int tipc_bcast_sync_rcv(struct net *net, struct tipc_link *l,
  236. struct tipc_msg *hdr)
  237. {
  238. struct sk_buff_head *inputq = &tipc_bc_base(net)->inputq;
  239. struct sk_buff_head xmitq;
  240. int rc = 0;
  241. __skb_queue_head_init(&xmitq);
  242. tipc_bcast_lock(net);
  243. if (msg_type(hdr) != STATE_MSG) {
  244. tipc_link_bc_init_rcv(l, hdr);
  245. } else if (!msg_bc_ack_invalid(hdr)) {
  246. tipc_link_bc_ack_rcv(l, msg_bcast_ack(hdr), &xmitq);
  247. rc = tipc_link_bc_sync_rcv(l, hdr, &xmitq);
  248. }
  249. tipc_bcast_unlock(net);
  250. tipc_bcbase_xmit(net, &xmitq);
  251. /* Any socket wakeup messages ? */
  252. if (!skb_queue_empty(inputq))
  253. tipc_sk_rcv(net, inputq);
  254. return rc;
  255. }
  256. /* tipc_bcast_add_peer - add a peer node to broadcast link and bearer
  257. *
  258. * RCU is locked, node lock is set
  259. */
  260. void tipc_bcast_add_peer(struct net *net, struct tipc_link *uc_l,
  261. struct sk_buff_head *xmitq)
  262. {
  263. struct tipc_link *snd_l = tipc_bc_sndlink(net);
  264. tipc_bcast_lock(net);
  265. tipc_link_add_bc_peer(snd_l, uc_l, xmitq);
  266. tipc_bcbase_select_primary(net);
  267. tipc_bcast_unlock(net);
  268. }
  269. /* tipc_bcast_remove_peer - remove a peer node from broadcast link and bearer
  270. *
  271. * RCU is locked, node lock is set
  272. */
  273. void tipc_bcast_remove_peer(struct net *net, struct tipc_link *rcv_l)
  274. {
  275. struct tipc_link *snd_l = tipc_bc_sndlink(net);
  276. struct sk_buff_head *inputq = &tipc_bc_base(net)->inputq;
  277. struct sk_buff_head xmitq;
  278. __skb_queue_head_init(&xmitq);
  279. tipc_bcast_lock(net);
  280. tipc_link_remove_bc_peer(snd_l, rcv_l, &xmitq);
  281. tipc_bcbase_select_primary(net);
  282. tipc_bcast_unlock(net);
  283. tipc_bcbase_xmit(net, &xmitq);
  284. /* Any socket wakeup messages ? */
  285. if (!skb_queue_empty(inputq))
  286. tipc_sk_rcv(net, inputq);
  287. }
  288. int tipc_bclink_reset_stats(struct net *net)
  289. {
  290. struct tipc_link *l = tipc_bc_sndlink(net);
  291. if (!l)
  292. return -ENOPROTOOPT;
  293. tipc_bcast_lock(net);
  294. tipc_link_reset_stats(l);
  295. tipc_bcast_unlock(net);
  296. return 0;
  297. }
  298. static int tipc_bc_link_set_queue_limits(struct net *net, u32 limit)
  299. {
  300. struct tipc_link *l = tipc_bc_sndlink(net);
  301. if (!l)
  302. return -ENOPROTOOPT;
  303. if (limit < BCLINK_WIN_MIN)
  304. limit = BCLINK_WIN_MIN;
  305. if (limit > TIPC_MAX_LINK_WIN)
  306. return -EINVAL;
  307. tipc_bcast_lock(net);
  308. tipc_link_set_queue_limits(l, limit);
  309. tipc_bcast_unlock(net);
  310. return 0;
  311. }
  312. int tipc_nl_bc_link_set(struct net *net, struct nlattr *attrs[])
  313. {
  314. int err;
  315. u32 win;
  316. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  317. if (!attrs[TIPC_NLA_LINK_PROP])
  318. return -EINVAL;
  319. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP], props);
  320. if (err)
  321. return err;
  322. if (!props[TIPC_NLA_PROP_WIN])
  323. return -EOPNOTSUPP;
  324. win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  325. return tipc_bc_link_set_queue_limits(net, win);
  326. }
  327. int tipc_bcast_init(struct net *net)
  328. {
  329. struct tipc_net *tn = tipc_net(net);
  330. struct tipc_bc_base *bb = NULL;
  331. struct tipc_link *l = NULL;
  332. bb = kzalloc(sizeof(*bb), GFP_ATOMIC);
  333. if (!bb)
  334. goto enomem;
  335. tn->bcbase = bb;
  336. spin_lock_init(&tipc_net(net)->bclock);
  337. if (!tipc_link_bc_create(net, 0, 0,
  338. U16_MAX,
  339. BCLINK_WIN_DEFAULT,
  340. 0,
  341. &bb->inputq,
  342. NULL,
  343. NULL,
  344. &l))
  345. goto enomem;
  346. bb->link = l;
  347. tn->bcl = l;
  348. return 0;
  349. enomem:
  350. kfree(bb);
  351. kfree(l);
  352. return -ENOMEM;
  353. }
  354. void tipc_bcast_stop(struct net *net)
  355. {
  356. struct tipc_net *tn = net_generic(net, tipc_net_id);
  357. synchronize_net();
  358. kfree(tn->bcbase);
  359. kfree(tn->bcl);
  360. }