bcast.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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, u32 acked)
  214. {
  215. struct sk_buff_head *inputq = &tipc_bc_base(net)->inputq;
  216. struct sk_buff_head xmitq;
  217. __skb_queue_head_init(&xmitq);
  218. tipc_bcast_lock(net);
  219. tipc_link_bc_ack_rcv(l, acked, &xmitq);
  220. tipc_bcast_unlock(net);
  221. tipc_bcbase_xmit(net, &xmitq);
  222. /* Any socket wakeup messages ? */
  223. if (!skb_queue_empty(inputq))
  224. tipc_sk_rcv(net, inputq);
  225. }
  226. /* tipc_bcast_synch_rcv - check and update rcv link with peer's send state
  227. *
  228. * RCU is locked, no other locks set
  229. */
  230. void tipc_bcast_sync_rcv(struct net *net, struct tipc_link *l,
  231. struct tipc_msg *hdr)
  232. {
  233. struct sk_buff_head *inputq = &tipc_bc_base(net)->inputq;
  234. struct sk_buff_head xmitq;
  235. __skb_queue_head_init(&xmitq);
  236. tipc_bcast_lock(net);
  237. if (msg_type(hdr) == STATE_MSG) {
  238. tipc_link_bc_ack_rcv(l, msg_bcast_ack(hdr), &xmitq);
  239. tipc_link_bc_sync_rcv(l, hdr, &xmitq);
  240. } else {
  241. tipc_link_bc_init_rcv(l, hdr);
  242. }
  243. tipc_bcast_unlock(net);
  244. tipc_bcbase_xmit(net, &xmitq);
  245. /* Any socket wakeup messages ? */
  246. if (!skb_queue_empty(inputq))
  247. tipc_sk_rcv(net, inputq);
  248. }
  249. /* tipc_bcast_add_peer - add a peer node to broadcast link and bearer
  250. *
  251. * RCU is locked, node lock is set
  252. */
  253. void tipc_bcast_add_peer(struct net *net, struct tipc_link *uc_l,
  254. struct sk_buff_head *xmitq)
  255. {
  256. struct tipc_link *snd_l = tipc_bc_sndlink(net);
  257. tipc_bcast_lock(net);
  258. tipc_link_add_bc_peer(snd_l, uc_l, xmitq);
  259. tipc_bcbase_select_primary(net);
  260. tipc_bcast_unlock(net);
  261. }
  262. /* tipc_bcast_remove_peer - remove a peer node from broadcast link and bearer
  263. *
  264. * RCU is locked, node lock is set
  265. */
  266. void tipc_bcast_remove_peer(struct net *net, struct tipc_link *rcv_l)
  267. {
  268. struct tipc_link *snd_l = tipc_bc_sndlink(net);
  269. struct sk_buff_head *inputq = &tipc_bc_base(net)->inputq;
  270. struct sk_buff_head xmitq;
  271. __skb_queue_head_init(&xmitq);
  272. tipc_bcast_lock(net);
  273. tipc_link_remove_bc_peer(snd_l, rcv_l, &xmitq);
  274. tipc_bcbase_select_primary(net);
  275. tipc_bcast_unlock(net);
  276. tipc_bcbase_xmit(net, &xmitq);
  277. /* Any socket wakeup messages ? */
  278. if (!skb_queue_empty(inputq))
  279. tipc_sk_rcv(net, inputq);
  280. }
  281. static int __tipc_nl_add_bc_link_stat(struct sk_buff *skb,
  282. struct tipc_stats *stats)
  283. {
  284. int i;
  285. struct nlattr *nest;
  286. struct nla_map {
  287. __u32 key;
  288. __u32 val;
  289. };
  290. struct nla_map map[] = {
  291. {TIPC_NLA_STATS_RX_INFO, stats->recv_info},
  292. {TIPC_NLA_STATS_RX_FRAGMENTS, stats->recv_fragments},
  293. {TIPC_NLA_STATS_RX_FRAGMENTED, stats->recv_fragmented},
  294. {TIPC_NLA_STATS_RX_BUNDLES, stats->recv_bundles},
  295. {TIPC_NLA_STATS_RX_BUNDLED, stats->recv_bundled},
  296. {TIPC_NLA_STATS_TX_INFO, stats->sent_info},
  297. {TIPC_NLA_STATS_TX_FRAGMENTS, stats->sent_fragments},
  298. {TIPC_NLA_STATS_TX_FRAGMENTED, stats->sent_fragmented},
  299. {TIPC_NLA_STATS_TX_BUNDLES, stats->sent_bundles},
  300. {TIPC_NLA_STATS_TX_BUNDLED, stats->sent_bundled},
  301. {TIPC_NLA_STATS_RX_NACKS, stats->recv_nacks},
  302. {TIPC_NLA_STATS_RX_DEFERRED, stats->deferred_recv},
  303. {TIPC_NLA_STATS_TX_NACKS, stats->sent_nacks},
  304. {TIPC_NLA_STATS_TX_ACKS, stats->sent_acks},
  305. {TIPC_NLA_STATS_RETRANSMITTED, stats->retransmitted},
  306. {TIPC_NLA_STATS_DUPLICATES, stats->duplicates},
  307. {TIPC_NLA_STATS_LINK_CONGS, stats->link_congs},
  308. {TIPC_NLA_STATS_MAX_QUEUE, stats->max_queue_sz},
  309. {TIPC_NLA_STATS_AVG_QUEUE, stats->queue_sz_counts ?
  310. (stats->accu_queue_sz / stats->queue_sz_counts) : 0}
  311. };
  312. nest = nla_nest_start(skb, TIPC_NLA_LINK_STATS);
  313. if (!nest)
  314. return -EMSGSIZE;
  315. for (i = 0; i < ARRAY_SIZE(map); i++)
  316. if (nla_put_u32(skb, map[i].key, map[i].val))
  317. goto msg_full;
  318. nla_nest_end(skb, nest);
  319. return 0;
  320. msg_full:
  321. nla_nest_cancel(skb, nest);
  322. return -EMSGSIZE;
  323. }
  324. int tipc_nl_add_bc_link(struct net *net, struct tipc_nl_msg *msg)
  325. {
  326. int err;
  327. void *hdr;
  328. struct nlattr *attrs;
  329. struct nlattr *prop;
  330. struct tipc_net *tn = net_generic(net, tipc_net_id);
  331. struct tipc_link *bcl = tn->bcl;
  332. if (!bcl)
  333. return 0;
  334. tipc_bcast_lock(net);
  335. hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, &tipc_genl_family,
  336. NLM_F_MULTI, TIPC_NL_LINK_GET);
  337. if (!hdr) {
  338. tipc_bcast_unlock(net);
  339. return -EMSGSIZE;
  340. }
  341. attrs = nla_nest_start(msg->skb, TIPC_NLA_LINK);
  342. if (!attrs)
  343. goto msg_full;
  344. /* The broadcast link is always up */
  345. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_UP))
  346. goto attr_msg_full;
  347. if (nla_put_flag(msg->skb, TIPC_NLA_LINK_BROADCAST))
  348. goto attr_msg_full;
  349. if (nla_put_string(msg->skb, TIPC_NLA_LINK_NAME, bcl->name))
  350. goto attr_msg_full;
  351. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, bcl->rcv_nxt))
  352. goto attr_msg_full;
  353. if (nla_put_u32(msg->skb, TIPC_NLA_LINK_TX, bcl->snd_nxt))
  354. goto attr_msg_full;
  355. prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
  356. if (!prop)
  357. goto attr_msg_full;
  358. if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bcl->window))
  359. goto prop_msg_full;
  360. nla_nest_end(msg->skb, prop);
  361. err = __tipc_nl_add_bc_link_stat(msg->skb, &bcl->stats);
  362. if (err)
  363. goto attr_msg_full;
  364. tipc_bcast_unlock(net);
  365. nla_nest_end(msg->skb, attrs);
  366. genlmsg_end(msg->skb, hdr);
  367. return 0;
  368. prop_msg_full:
  369. nla_nest_cancel(msg->skb, prop);
  370. attr_msg_full:
  371. nla_nest_cancel(msg->skb, attrs);
  372. msg_full:
  373. tipc_bcast_unlock(net);
  374. genlmsg_cancel(msg->skb, hdr);
  375. return -EMSGSIZE;
  376. }
  377. int tipc_bclink_reset_stats(struct net *net)
  378. {
  379. struct tipc_net *tn = net_generic(net, tipc_net_id);
  380. struct tipc_link *bcl = tn->bcl;
  381. if (!bcl)
  382. return -ENOPROTOOPT;
  383. tipc_bcast_lock(net);
  384. memset(&bcl->stats, 0, sizeof(bcl->stats));
  385. tipc_bcast_unlock(net);
  386. return 0;
  387. }
  388. static int tipc_bc_link_set_queue_limits(struct net *net, u32 limit)
  389. {
  390. struct tipc_link *l = tipc_bc_sndlink(net);
  391. if (!l)
  392. return -ENOPROTOOPT;
  393. if (limit < BCLINK_WIN_MIN)
  394. limit = BCLINK_WIN_MIN;
  395. if (limit > TIPC_MAX_LINK_WIN)
  396. return -EINVAL;
  397. tipc_bcast_lock(net);
  398. tipc_link_set_queue_limits(l, limit);
  399. tipc_bcast_unlock(net);
  400. return 0;
  401. }
  402. int tipc_nl_bc_link_set(struct net *net, struct nlattr *attrs[])
  403. {
  404. int err;
  405. u32 win;
  406. struct nlattr *props[TIPC_NLA_PROP_MAX + 1];
  407. if (!attrs[TIPC_NLA_LINK_PROP])
  408. return -EINVAL;
  409. err = tipc_nl_parse_link_prop(attrs[TIPC_NLA_LINK_PROP], props);
  410. if (err)
  411. return err;
  412. if (!props[TIPC_NLA_PROP_WIN])
  413. return -EOPNOTSUPP;
  414. win = nla_get_u32(props[TIPC_NLA_PROP_WIN]);
  415. return tipc_bc_link_set_queue_limits(net, win);
  416. }
  417. int tipc_bcast_init(struct net *net)
  418. {
  419. struct tipc_net *tn = tipc_net(net);
  420. struct tipc_bc_base *bb = NULL;
  421. struct tipc_link *l = NULL;
  422. bb = kzalloc(sizeof(*bb), GFP_ATOMIC);
  423. if (!bb)
  424. goto enomem;
  425. tn->bcbase = bb;
  426. spin_lock_init(&tipc_net(net)->bclock);
  427. if (!tipc_link_bc_create(net, 0, 0,
  428. U16_MAX,
  429. BCLINK_WIN_DEFAULT,
  430. 0,
  431. &bb->inputq,
  432. NULL,
  433. NULL,
  434. &l))
  435. goto enomem;
  436. bb->link = l;
  437. tn->bcl = l;
  438. return 0;
  439. enomem:
  440. kfree(bb);
  441. kfree(l);
  442. return -ENOMEM;
  443. }
  444. void tipc_bcast_reinit(struct net *net)
  445. {
  446. struct tipc_bc_base *b = tipc_bc_base(net);
  447. msg_set_prevnode(b->link->pmsg, tipc_own_addr(net));
  448. }
  449. void tipc_bcast_stop(struct net *net)
  450. {
  451. struct tipc_net *tn = net_generic(net, tipc_net_id);
  452. synchronize_net();
  453. kfree(tn->bcbase);
  454. kfree(tn->bcl);
  455. }