name_distr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 tipc_net *tn = net_generic(net, tipc_net_id);
  69. struct sk_buff *buf = tipc_buf_acquire(INT_H_SIZE + size);
  70. struct tipc_msg *msg;
  71. if (buf != NULL) {
  72. msg = buf_msg(buf);
  73. tipc_msg_init(tn->own_addr, msg, NAME_DISTRIBUTOR, type,
  74. INT_H_SIZE, dest);
  75. msg_set_size(msg, INT_H_SIZE + size);
  76. }
  77. return buf;
  78. }
  79. void named_cluster_distribute(struct net *net, struct sk_buff *skb)
  80. {
  81. struct tipc_net *tn = net_generic(net, tipc_net_id);
  82. struct sk_buff *oskb;
  83. struct tipc_node *node;
  84. u32 dnode;
  85. rcu_read_lock();
  86. list_for_each_entry_rcu(node, &tn->node_list, list) {
  87. dnode = node->addr;
  88. if (in_own_node(net, dnode))
  89. continue;
  90. if (!tipc_node_active_links(node))
  91. continue;
  92. oskb = pskb_copy(skb, GFP_ATOMIC);
  93. if (!oskb)
  94. break;
  95. msg_set_destnode(buf_msg(oskb), dnode);
  96. tipc_link_xmit_skb(net, oskb, dnode, dnode);
  97. }
  98. rcu_read_unlock();
  99. kfree_skb(skb);
  100. }
  101. /**
  102. * tipc_named_publish - tell other nodes about a new publication by this node
  103. */
  104. struct sk_buff *tipc_named_publish(struct net *net, struct publication *publ)
  105. {
  106. struct tipc_net *tn = net_generic(net, tipc_net_id);
  107. struct sk_buff *buf;
  108. struct distr_item *item;
  109. list_add_tail_rcu(&publ->local_list,
  110. &tn->nametbl->publ_list[publ->scope]);
  111. if (publ->scope == TIPC_NODE_SCOPE)
  112. return NULL;
  113. buf = named_prepare_buf(net, PUBLICATION, ITEM_SIZE, 0);
  114. if (!buf) {
  115. pr_warn("Publication distribution failure\n");
  116. return NULL;
  117. }
  118. item = (struct distr_item *)msg_data(buf_msg(buf));
  119. publ_to_item(item, publ);
  120. return buf;
  121. }
  122. /**
  123. * tipc_named_withdraw - tell other nodes about a withdrawn publication by this node
  124. */
  125. struct sk_buff *tipc_named_withdraw(struct net *net, struct publication *publ)
  126. {
  127. struct sk_buff *buf;
  128. struct distr_item *item;
  129. list_del(&publ->local_list);
  130. if (publ->scope == TIPC_NODE_SCOPE)
  131. return NULL;
  132. buf = named_prepare_buf(net, WITHDRAWAL, ITEM_SIZE, 0);
  133. if (!buf) {
  134. pr_warn("Withdrawal distribution failure\n");
  135. return NULL;
  136. }
  137. item = (struct distr_item *)msg_data(buf_msg(buf));
  138. publ_to_item(item, publ);
  139. return buf;
  140. }
  141. /**
  142. * named_distribute - prepare name info for bulk distribution to another node
  143. * @list: list of messages (buffers) to be returned from this function
  144. * @dnode: node to be updated
  145. * @pls: linked list of publication items to be packed into buffer chain
  146. */
  147. static void named_distribute(struct net *net, struct sk_buff_head *list,
  148. u32 dnode, struct list_head *pls)
  149. {
  150. struct publication *publ;
  151. struct sk_buff *skb = NULL;
  152. struct distr_item *item = NULL;
  153. uint msg_dsz = (tipc_node_get_mtu(net, dnode, 0) / ITEM_SIZE) *
  154. ITEM_SIZE;
  155. uint msg_rem = msg_dsz;
  156. list_for_each_entry(publ, pls, local_list) {
  157. /* Prepare next buffer: */
  158. if (!skb) {
  159. skb = named_prepare_buf(net, PUBLICATION, msg_rem,
  160. dnode);
  161. if (!skb) {
  162. pr_warn("Bulk publication failure\n");
  163. return;
  164. }
  165. item = (struct distr_item *)msg_data(buf_msg(skb));
  166. }
  167. /* Pack publication into message: */
  168. publ_to_item(item, publ);
  169. item++;
  170. msg_rem -= ITEM_SIZE;
  171. /* Append full buffer to list: */
  172. if (!msg_rem) {
  173. __skb_queue_tail(list, skb);
  174. skb = NULL;
  175. msg_rem = msg_dsz;
  176. }
  177. }
  178. if (skb) {
  179. msg_set_size(buf_msg(skb), INT_H_SIZE + (msg_dsz - msg_rem));
  180. skb_trim(skb, INT_H_SIZE + (msg_dsz - msg_rem));
  181. __skb_queue_tail(list, skb);
  182. }
  183. }
  184. /**
  185. * tipc_named_node_up - tell specified node about all publications by this node
  186. */
  187. void tipc_named_node_up(struct net *net, u32 dnode)
  188. {
  189. struct tipc_net *tn = net_generic(net, tipc_net_id);
  190. struct sk_buff_head head;
  191. __skb_queue_head_init(&head);
  192. rcu_read_lock();
  193. named_distribute(net, &head, dnode,
  194. &tn->nametbl->publ_list[TIPC_CLUSTER_SCOPE]);
  195. named_distribute(net, &head, dnode,
  196. &tn->nametbl->publ_list[TIPC_ZONE_SCOPE]);
  197. rcu_read_unlock();
  198. tipc_link_xmit(net, &head, dnode, dnode);
  199. }
  200. static void tipc_publ_subscribe(struct net *net, struct publication *publ,
  201. u32 addr)
  202. {
  203. struct tipc_node *node;
  204. if (in_own_node(net, addr))
  205. return;
  206. node = tipc_node_find(net, addr);
  207. if (!node) {
  208. pr_warn("Node subscription rejected, unknown node 0x%x\n",
  209. addr);
  210. return;
  211. }
  212. tipc_node_lock(node);
  213. list_add_tail(&publ->nodesub_list, &node->publ_list);
  214. tipc_node_unlock(node);
  215. tipc_node_put(node);
  216. }
  217. static void tipc_publ_unsubscribe(struct net *net, struct publication *publ,
  218. u32 addr)
  219. {
  220. struct tipc_node *node;
  221. node = tipc_node_find(net, addr);
  222. if (!node)
  223. return;
  224. tipc_node_lock(node);
  225. list_del_init(&publ->nodesub_list);
  226. tipc_node_unlock(node);
  227. tipc_node_put(node);
  228. }
  229. /**
  230. * tipc_publ_purge - remove publication associated with a failed node
  231. *
  232. * Invoked for each publication issued by a newly failed node.
  233. * Removes publication structure from name table & deletes it.
  234. */
  235. static void tipc_publ_purge(struct net *net, struct publication *publ, u32 addr)
  236. {
  237. struct tipc_net *tn = net_generic(net, tipc_net_id);
  238. struct publication *p;
  239. spin_lock_bh(&tn->nametbl_lock);
  240. p = tipc_nametbl_remove_publ(net, publ->type, publ->lower,
  241. publ->node, publ->ref, publ->key);
  242. if (p)
  243. tipc_publ_unsubscribe(net, p, addr);
  244. spin_unlock_bh(&tn->nametbl_lock);
  245. if (p != publ) {
  246. pr_err("Unable to remove publication from failed node\n"
  247. " (type=%u, lower=%u, node=0x%x, ref=%u, key=%u)\n",
  248. publ->type, publ->lower, publ->node, publ->ref,
  249. publ->key);
  250. }
  251. kfree_rcu(p, rcu);
  252. }
  253. void tipc_publ_notify(struct net *net, struct list_head *nsub_list, u32 addr)
  254. {
  255. struct publication *publ, *tmp;
  256. list_for_each_entry_safe(publ, tmp, nsub_list, nodesub_list)
  257. tipc_publ_purge(net, publ, addr);
  258. }
  259. /**
  260. * tipc_update_nametbl - try to process a nametable update and notify
  261. * subscribers
  262. *
  263. * tipc_nametbl_lock must be held.
  264. * Returns the publication item if successful, otherwise NULL.
  265. */
  266. static bool tipc_update_nametbl(struct net *net, struct distr_item *i,
  267. u32 node, u32 dtype)
  268. {
  269. struct publication *publ = NULL;
  270. if (dtype == PUBLICATION) {
  271. publ = tipc_nametbl_insert_publ(net, ntohl(i->type),
  272. ntohl(i->lower),
  273. ntohl(i->upper),
  274. TIPC_CLUSTER_SCOPE, node,
  275. ntohl(i->ref), ntohl(i->key));
  276. if (publ) {
  277. tipc_publ_subscribe(net, publ, node);
  278. return true;
  279. }
  280. } else if (dtype == WITHDRAWAL) {
  281. publ = tipc_nametbl_remove_publ(net, ntohl(i->type),
  282. ntohl(i->lower),
  283. node, ntohl(i->ref),
  284. ntohl(i->key));
  285. if (publ) {
  286. tipc_publ_unsubscribe(net, publ, node);
  287. kfree_rcu(publ, rcu);
  288. return true;
  289. }
  290. } else {
  291. pr_warn("Unrecognized name table message received\n");
  292. }
  293. return false;
  294. }
  295. /**
  296. * tipc_named_add_backlog - add a failed name table update to the backlog
  297. *
  298. */
  299. static void tipc_named_add_backlog(struct distr_item *i, u32 type, u32 node)
  300. {
  301. struct distr_queue_item *e;
  302. unsigned long now = get_jiffies_64();
  303. e = kzalloc(sizeof(*e), GFP_ATOMIC);
  304. if (!e)
  305. return;
  306. e->dtype = type;
  307. e->node = node;
  308. e->expires = now + msecs_to_jiffies(sysctl_tipc_named_timeout);
  309. memcpy(e, i, sizeof(*i));
  310. list_add_tail(&e->next, &tipc_dist_queue);
  311. }
  312. /**
  313. * tipc_named_process_backlog - try to process any pending name table updates
  314. * from the network.
  315. */
  316. void tipc_named_process_backlog(struct net *net)
  317. {
  318. struct distr_queue_item *e, *tmp;
  319. char addr[16];
  320. unsigned long now = get_jiffies_64();
  321. list_for_each_entry_safe(e, tmp, &tipc_dist_queue, next) {
  322. if (time_after(e->expires, now)) {
  323. if (!tipc_update_nametbl(net, &e->i, e->node, e->dtype))
  324. continue;
  325. } else {
  326. tipc_addr_string_fill(addr, e->node);
  327. pr_warn_ratelimited("Dropping name table update (%d) of {%u, %u, %u} from %s key=%u\n",
  328. e->dtype, ntohl(e->i.type),
  329. ntohl(e->i.lower),
  330. ntohl(e->i.upper),
  331. addr, ntohl(e->i.key));
  332. }
  333. list_del(&e->next);
  334. kfree(e);
  335. }
  336. }
  337. /**
  338. * tipc_named_rcv - process name table update messages sent by another node
  339. */
  340. void tipc_named_rcv(struct net *net, struct sk_buff_head *inputq)
  341. {
  342. struct tipc_net *tn = net_generic(net, tipc_net_id);
  343. struct tipc_msg *msg;
  344. struct distr_item *item;
  345. uint count;
  346. u32 node;
  347. struct sk_buff *skb;
  348. int mtype;
  349. spin_lock_bh(&tn->nametbl_lock);
  350. for (skb = skb_dequeue(inputq); skb; skb = skb_dequeue(inputq)) {
  351. msg = buf_msg(skb);
  352. mtype = msg_type(msg);
  353. item = (struct distr_item *)msg_data(msg);
  354. count = msg_data_sz(msg) / ITEM_SIZE;
  355. node = msg_orignode(msg);
  356. while (count--) {
  357. if (!tipc_update_nametbl(net, item, node, mtype))
  358. tipc_named_add_backlog(item, mtype, node);
  359. item++;
  360. }
  361. kfree_skb(skb);
  362. tipc_named_process_backlog(net);
  363. }
  364. spin_unlock_bh(&tn->nametbl_lock);
  365. }
  366. /**
  367. * tipc_named_reinit - re-initialize local publications
  368. *
  369. * This routine is called whenever TIPC networking is enabled.
  370. * All name table entries published by this node are updated to reflect
  371. * the node's new network address.
  372. */
  373. void tipc_named_reinit(struct net *net)
  374. {
  375. struct tipc_net *tn = net_generic(net, tipc_net_id);
  376. struct publication *publ;
  377. int scope;
  378. spin_lock_bh(&tn->nametbl_lock);
  379. for (scope = TIPC_ZONE_SCOPE; scope <= TIPC_NODE_SCOPE; scope++)
  380. list_for_each_entry_rcu(publ, &tn->nametbl->publ_list[scope],
  381. local_list)
  382. publ->node = tn->own_addr;
  383. spin_unlock_bh(&tn->nametbl_lock);
  384. }