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