name_distr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * net/tipc/name_distr.c: TIPC name distribution code
  3. *
  4. * Copyright (c) 2000-2006, 2014, Ericsson AB
  5. * Copyright (c) 2005, 2010-2011, Wind River Systems
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the names of the copyright holders nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * Alternatively, this software may be distributed under the terms of the
  21. * GNU General Public License ("GPL") version 2 as published by the Free
  22. * Software Foundation.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include "core.h"
  37. #include "link.h"
  38. #include "name_distr.h"
  39. int sysctl_tipc_named_timeout __read_mostly = 2000;
  40. /**
  41. * struct tipc_dist_queue - queue holding deferred name table updates
  42. */
  43. static struct list_head tipc_dist_queue = LIST_HEAD_INIT(tipc_dist_queue);
  44. struct distr_queue_item {
  45. struct distr_item i;
  46. u32 dtype;
  47. u32 node;
  48. unsigned long expires;
  49. struct list_head next;
  50. };
  51. /**
  52. * publ_to_item - add publication info to a publication message
  53. */
  54. static void publ_to_item(struct distr_item *i, struct publication *p)
  55. {
  56. i->type = htonl(p->type);
  57. i->lower = htonl(p->lower);
  58. i->upper = htonl(p->upper);
  59. i->ref = htonl(p->ref);
  60. i->key = htonl(p->key);
  61. }
  62. /**
  63. * named_prepare_buf - allocate & initialize a publication message
  64. */
  65. static struct sk_buff *named_prepare_buf(struct net *net, u32 type, u32 size,
  66. u32 dest)
  67. {
  68. struct sk_buff *buf = tipc_buf_acquire(INT_H_SIZE + size);
  69. struct tipc_msg *msg;
  70. if (buf != NULL) {
  71. msg = buf_msg(buf);
  72. tipc_msg_init(net, msg, NAME_DISTRIBUTOR, type, INT_H_SIZE,
  73. dest);
  74. msg_set_size(msg, INT_H_SIZE + size);
  75. }
  76. return buf;
  77. }
  78. void named_cluster_distribute(struct net *net, struct sk_buff *skb)
  79. {
  80. struct tipc_net *tn = net_generic(net, tipc_net_id);
  81. struct sk_buff *oskb;
  82. struct tipc_node *node;
  83. u32 dnode;
  84. rcu_read_lock();
  85. list_for_each_entry_rcu(node, &tn->node_list, list) {
  86. dnode = node->addr;
  87. if (in_own_node(net, dnode))
  88. continue;
  89. if (!tipc_node_active_links(node))
  90. continue;
  91. oskb = skb_copy(skb, GFP_ATOMIC);
  92. if (!oskb)
  93. break;
  94. msg_set_destnode(buf_msg(oskb), dnode);
  95. tipc_link_xmit_skb(net, oskb, dnode, dnode);
  96. }
  97. rcu_read_unlock();
  98. kfree_skb(skb);
  99. }
  100. /**
  101. * tipc_named_publish - tell other nodes about a new publication by this node
  102. */
  103. struct sk_buff *tipc_named_publish(struct net *net, struct publication *publ)
  104. {
  105. struct tipc_net *tn = net_generic(net, tipc_net_id);
  106. struct sk_buff *buf;
  107. struct distr_item *item;
  108. list_add_tail_rcu(&publ->local_list,
  109. &tn->nametbl->publ_list[publ->scope]);
  110. if (publ->scope == TIPC_NODE_SCOPE)
  111. return NULL;
  112. buf = named_prepare_buf(net, PUBLICATION, ITEM_SIZE, 0);
  113. if (!buf) {
  114. pr_warn("Publication distribution failure\n");
  115. return NULL;
  116. }
  117. item = (struct distr_item *)msg_data(buf_msg(buf));
  118. publ_to_item(item, publ);
  119. return buf;
  120. }
  121. /**
  122. * tipc_named_withdraw - tell other nodes about a withdrawn publication by this node
  123. */
  124. struct sk_buff *tipc_named_withdraw(struct net *net, struct publication *publ)
  125. {
  126. struct sk_buff *buf;
  127. struct distr_item *item;
  128. list_del(&publ->local_list);
  129. if (publ->scope == TIPC_NODE_SCOPE)
  130. return NULL;
  131. buf = named_prepare_buf(net, WITHDRAWAL, ITEM_SIZE, 0);
  132. if (!buf) {
  133. pr_warn("Withdrawal distribution failure\n");
  134. return NULL;
  135. }
  136. item = (struct distr_item *)msg_data(buf_msg(buf));
  137. publ_to_item(item, publ);
  138. return buf;
  139. }
  140. /**
  141. * named_distribute - prepare name info for bulk distribution to another node
  142. * @list: list of messages (buffers) to be returned from this function
  143. * @dnode: node to be updated
  144. * @pls: linked list of publication items to be packed into buffer chain
  145. */
  146. static void named_distribute(struct net *net, struct sk_buff_head *list,
  147. u32 dnode, struct list_head *pls)
  148. {
  149. struct publication *publ;
  150. struct sk_buff *skb = NULL;
  151. struct distr_item *item = NULL;
  152. uint msg_dsz = (tipc_node_get_mtu(net, dnode, 0) / ITEM_SIZE) *
  153. ITEM_SIZE;
  154. uint msg_rem = msg_dsz;
  155. list_for_each_entry(publ, pls, local_list) {
  156. /* Prepare next buffer: */
  157. if (!skb) {
  158. skb = named_prepare_buf(net, PUBLICATION, msg_rem,
  159. dnode);
  160. if (!skb) {
  161. pr_warn("Bulk publication failure\n");
  162. return;
  163. }
  164. item = (struct distr_item *)msg_data(buf_msg(skb));
  165. }
  166. /* Pack publication into message: */
  167. publ_to_item(item, publ);
  168. item++;
  169. msg_rem -= ITEM_SIZE;
  170. /* Append full buffer to list: */
  171. if (!msg_rem) {
  172. __skb_queue_tail(list, skb);
  173. skb = NULL;
  174. msg_rem = msg_dsz;
  175. }
  176. }
  177. if (skb) {
  178. msg_set_size(buf_msg(skb), INT_H_SIZE + (msg_dsz - msg_rem));
  179. skb_trim(skb, INT_H_SIZE + (msg_dsz - msg_rem));
  180. __skb_queue_tail(list, skb);
  181. }
  182. }
  183. /**
  184. * tipc_named_node_up - tell specified node about all publications by this node
  185. */
  186. void tipc_named_node_up(struct net *net, u32 dnode)
  187. {
  188. struct tipc_net *tn = net_generic(net, tipc_net_id);
  189. struct sk_buff_head head;
  190. __skb_queue_head_init(&head);
  191. rcu_read_lock();
  192. named_distribute(net, &head, dnode,
  193. &tn->nametbl->publ_list[TIPC_CLUSTER_SCOPE]);
  194. named_distribute(net, &head, dnode,
  195. &tn->nametbl->publ_list[TIPC_ZONE_SCOPE]);
  196. rcu_read_unlock();
  197. tipc_link_xmit(net, &head, dnode, dnode);
  198. }
  199. static void tipc_publ_subscribe(struct net *net, struct publication *publ,
  200. u32 addr)
  201. {
  202. struct tipc_node *node;
  203. if (in_own_node(net, addr))
  204. return;
  205. node = tipc_node_find(net, addr);
  206. if (!node) {
  207. pr_warn("Node subscription rejected, unknown node 0x%x\n",
  208. addr);
  209. return;
  210. }
  211. tipc_node_lock(node);
  212. list_add_tail(&publ->nodesub_list, &node->publ_list);
  213. tipc_node_unlock(node);
  214. }
  215. static void tipc_publ_unsubscribe(struct net *net, struct publication *publ,
  216. u32 addr)
  217. {
  218. struct tipc_node *node;
  219. node = tipc_node_find(net, addr);
  220. if (!node)
  221. return;
  222. tipc_node_lock(node);
  223. list_del_init(&publ->nodesub_list);
  224. tipc_node_unlock(node);
  225. }
  226. /**
  227. * tipc_publ_purge - remove publication associated with a failed node
  228. *
  229. * Invoked for each publication issued by a newly failed node.
  230. * Removes publication structure from name table & deletes it.
  231. */
  232. static void tipc_publ_purge(struct net *net, struct publication *publ, u32 addr)
  233. {
  234. struct tipc_net *tn = net_generic(net, tipc_net_id);
  235. struct publication *p;
  236. spin_lock_bh(&tn->nametbl_lock);
  237. p = tipc_nametbl_remove_publ(net, publ->type, publ->lower,
  238. publ->node, publ->ref, publ->key);
  239. if (p)
  240. tipc_publ_unsubscribe(net, p, addr);
  241. spin_unlock_bh(&tn->nametbl_lock);
  242. if (p != publ) {
  243. pr_err("Unable to remove publication from failed node\n"
  244. " (type=%u, lower=%u, node=0x%x, ref=%u, key=%u)\n",
  245. publ->type, publ->lower, publ->node, publ->ref,
  246. publ->key);
  247. }
  248. kfree_rcu(p, rcu);
  249. }
  250. void tipc_publ_notify(struct net *net, struct list_head *nsub_list, u32 addr)
  251. {
  252. struct publication *publ, *tmp;
  253. list_for_each_entry_safe(publ, tmp, nsub_list, nodesub_list)
  254. tipc_publ_purge(net, publ, addr);
  255. }
  256. /**
  257. * tipc_update_nametbl - try to process a nametable update and notify
  258. * subscribers
  259. *
  260. * tipc_nametbl_lock must be held.
  261. * Returns the publication item if successful, otherwise NULL.
  262. */
  263. static bool tipc_update_nametbl(struct net *net, struct distr_item *i,
  264. u32 node, u32 dtype)
  265. {
  266. struct publication *publ = NULL;
  267. if (dtype == PUBLICATION) {
  268. publ = tipc_nametbl_insert_publ(net, ntohl(i->type),
  269. ntohl(i->lower),
  270. ntohl(i->upper),
  271. TIPC_CLUSTER_SCOPE, node,
  272. ntohl(i->ref), ntohl(i->key));
  273. if (publ) {
  274. tipc_publ_subscribe(net, publ, node);
  275. return true;
  276. }
  277. } else if (dtype == WITHDRAWAL) {
  278. publ = tipc_nametbl_remove_publ(net, ntohl(i->type),
  279. ntohl(i->lower),
  280. node, ntohl(i->ref),
  281. ntohl(i->key));
  282. if (publ) {
  283. tipc_publ_unsubscribe(net, publ, node);
  284. kfree_rcu(publ, rcu);
  285. return true;
  286. }
  287. } else {
  288. pr_warn("Unrecognized name table message received\n");
  289. }
  290. return false;
  291. }
  292. /**
  293. * tipc_named_add_backlog - add a failed name table update to the backlog
  294. *
  295. */
  296. static void tipc_named_add_backlog(struct distr_item *i, u32 type, u32 node)
  297. {
  298. struct distr_queue_item *e;
  299. unsigned long now = get_jiffies_64();
  300. e = kzalloc(sizeof(*e), GFP_ATOMIC);
  301. if (!e)
  302. return;
  303. e->dtype = type;
  304. e->node = node;
  305. e->expires = now + msecs_to_jiffies(sysctl_tipc_named_timeout);
  306. memcpy(e, i, sizeof(*i));
  307. list_add_tail(&e->next, &tipc_dist_queue);
  308. }
  309. /**
  310. * tipc_named_process_backlog - try to process any pending name table updates
  311. * from the network.
  312. */
  313. void tipc_named_process_backlog(struct net *net)
  314. {
  315. struct distr_queue_item *e, *tmp;
  316. char addr[16];
  317. unsigned long now = get_jiffies_64();
  318. list_for_each_entry_safe(e, tmp, &tipc_dist_queue, next) {
  319. if (time_after(e->expires, now)) {
  320. if (!tipc_update_nametbl(net, &e->i, e->node, e->dtype))
  321. continue;
  322. } else {
  323. tipc_addr_string_fill(addr, e->node);
  324. pr_warn_ratelimited("Dropping name table update (%d) of {%u, %u, %u} from %s key=%u\n",
  325. e->dtype, ntohl(e->i.type),
  326. ntohl(e->i.lower),
  327. ntohl(e->i.upper),
  328. addr, ntohl(e->i.key));
  329. }
  330. list_del(&e->next);
  331. kfree(e);
  332. }
  333. }
  334. /**
  335. * tipc_named_rcv - process name table update message sent by another node
  336. */
  337. void tipc_named_rcv(struct net *net, struct sk_buff *buf)
  338. {
  339. struct tipc_net *tn = net_generic(net, tipc_net_id);
  340. struct tipc_msg *msg = buf_msg(buf);
  341. struct distr_item *item = (struct distr_item *)msg_data(msg);
  342. u32 count = msg_data_sz(msg) / ITEM_SIZE;
  343. u32 node = msg_orignode(msg);
  344. spin_lock_bh(&tn->nametbl_lock);
  345. while (count--) {
  346. if (!tipc_update_nametbl(net, item, node, msg_type(msg)))
  347. tipc_named_add_backlog(item, msg_type(msg), node);
  348. item++;
  349. }
  350. tipc_named_process_backlog(net);
  351. spin_unlock_bh(&tn->nametbl_lock);
  352. kfree_skb(buf);
  353. }
  354. /**
  355. * tipc_named_reinit - re-initialize local publications
  356. *
  357. * This routine is called whenever TIPC networking is enabled.
  358. * All name table entries published by this node are updated to reflect
  359. * the node's new network address.
  360. */
  361. void tipc_named_reinit(struct net *net)
  362. {
  363. struct tipc_net *tn = net_generic(net, tipc_net_id);
  364. struct publication *publ;
  365. int scope;
  366. spin_lock_bh(&tn->nametbl_lock);
  367. for (scope = TIPC_ZONE_SCOPE; scope <= TIPC_NODE_SCOPE; scope++)
  368. list_for_each_entry_rcu(publ, &tn->nametbl->publ_list[scope],
  369. local_list)
  370. publ->node = tn->own_addr;
  371. spin_unlock_bh(&tn->nametbl_lock);
  372. }