name_distr.c 11 KB

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