name_table.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. /*
  2. * net/tipc/name_table.c: TIPC name table code
  3. *
  4. * Copyright (c) 2000-2006, Ericsson AB
  5. * Copyright (c) 2004-2008, 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 "config.h"
  38. #include "name_table.h"
  39. #include "name_distr.h"
  40. #include "subscr.h"
  41. #include "port.h"
  42. static int tipc_nametbl_size = 1024; /* must be a power of 2 */
  43. /**
  44. * struct sub_seq - container for all published instances of a name sequence
  45. * @lower: name sequence lower bound
  46. * @upper: name sequence upper bound
  47. * @node_list: circular list of publications made by own node
  48. * @cluster_list: circular list of publications made by own cluster
  49. * @zone_list: circular list of publications made by own zone
  50. * @node_list_size: number of entries in "node_list"
  51. * @cluster_list_size: number of entries in "cluster_list"
  52. * @zone_list_size: number of entries in "zone_list"
  53. *
  54. * Note: The zone list always contains at least one entry, since all
  55. * publications of the associated name sequence belong to it.
  56. * (The cluster and node lists may be empty.)
  57. */
  58. struct sub_seq {
  59. u32 lower;
  60. u32 upper;
  61. struct publication *node_list;
  62. struct publication *cluster_list;
  63. struct publication *zone_list;
  64. u32 node_list_size;
  65. u32 cluster_list_size;
  66. u32 zone_list_size;
  67. };
  68. /**
  69. * struct name_seq - container for all published instances of a name type
  70. * @type: 32 bit 'type' value for name sequence
  71. * @sseq: pointer to dynamically-sized array of sub-sequences of this 'type';
  72. * sub-sequences are sorted in ascending order
  73. * @alloc: number of sub-sequences currently in array
  74. * @first_free: array index of first unused sub-sequence entry
  75. * @ns_list: links to adjacent name sequences in hash chain
  76. * @subscriptions: list of subscriptions for this 'type'
  77. * @lock: spinlock controlling access to publication lists of all sub-sequences
  78. */
  79. struct name_seq {
  80. u32 type;
  81. struct sub_seq *sseqs;
  82. u32 alloc;
  83. u32 first_free;
  84. struct hlist_node ns_list;
  85. struct list_head subscriptions;
  86. spinlock_t lock;
  87. };
  88. /**
  89. * struct name_table - table containing all existing port name publications
  90. * @types: pointer to fixed-sized array of name sequence lists,
  91. * accessed via hashing on 'type'; name sequence lists are *not* sorted
  92. * @local_publ_count: number of publications issued by this node
  93. */
  94. struct name_table {
  95. struct hlist_head *types;
  96. u32 local_publ_count;
  97. };
  98. static struct name_table table;
  99. static atomic_t rsv_publ_ok = ATOMIC_INIT(0);
  100. DEFINE_RWLOCK(tipc_nametbl_lock);
  101. static int hash(int x)
  102. {
  103. return x & (tipc_nametbl_size - 1);
  104. }
  105. /**
  106. * publ_create - create a publication structure
  107. */
  108. static struct publication *publ_create(u32 type, u32 lower, u32 upper,
  109. u32 scope, u32 node, u32 port_ref,
  110. u32 key)
  111. {
  112. struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
  113. if (publ == NULL) {
  114. warn("Publication creation failure, no memory\n");
  115. return NULL;
  116. }
  117. publ->type = type;
  118. publ->lower = lower;
  119. publ->upper = upper;
  120. publ->scope = scope;
  121. publ->node = node;
  122. publ->ref = port_ref;
  123. publ->key = key;
  124. INIT_LIST_HEAD(&publ->local_list);
  125. INIT_LIST_HEAD(&publ->pport_list);
  126. INIT_LIST_HEAD(&publ->subscr.nodesub_list);
  127. return publ;
  128. }
  129. /**
  130. * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
  131. */
  132. static struct sub_seq *tipc_subseq_alloc(u32 cnt)
  133. {
  134. struct sub_seq *sseq = kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC);
  135. return sseq;
  136. }
  137. /**
  138. * tipc_nameseq_create - create a name sequence structure for the specified 'type'
  139. *
  140. * Allocates a single sub-sequence structure and sets it to all 0's.
  141. */
  142. static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head)
  143. {
  144. struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC);
  145. struct sub_seq *sseq = tipc_subseq_alloc(1);
  146. if (!nseq || !sseq) {
  147. warn("Name sequence creation failed, no memory\n");
  148. kfree(nseq);
  149. kfree(sseq);
  150. return NULL;
  151. }
  152. spin_lock_init(&nseq->lock);
  153. nseq->type = type;
  154. nseq->sseqs = sseq;
  155. nseq->alloc = 1;
  156. INIT_HLIST_NODE(&nseq->ns_list);
  157. INIT_LIST_HEAD(&nseq->subscriptions);
  158. hlist_add_head(&nseq->ns_list, seq_head);
  159. return nseq;
  160. }
  161. /**
  162. * nameseq_find_subseq - find sub-sequence (if any) matching a name instance
  163. *
  164. * Very time-critical, so binary searches through sub-sequence array.
  165. */
  166. static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq,
  167. u32 instance)
  168. {
  169. struct sub_seq *sseqs = nseq->sseqs;
  170. int low = 0;
  171. int high = nseq->first_free - 1;
  172. int mid;
  173. while (low <= high) {
  174. mid = (low + high) / 2;
  175. if (instance < sseqs[mid].lower)
  176. high = mid - 1;
  177. else if (instance > sseqs[mid].upper)
  178. low = mid + 1;
  179. else
  180. return &sseqs[mid];
  181. }
  182. return NULL;
  183. }
  184. /**
  185. * nameseq_locate_subseq - determine position of name instance in sub-sequence
  186. *
  187. * Returns index in sub-sequence array of the entry that contains the specified
  188. * instance value; if no entry contains that value, returns the position
  189. * where a new entry for it would be inserted in the array.
  190. *
  191. * Note: Similar to binary search code for locating a sub-sequence.
  192. */
  193. static u32 nameseq_locate_subseq(struct name_seq *nseq, u32 instance)
  194. {
  195. struct sub_seq *sseqs = nseq->sseqs;
  196. int low = 0;
  197. int high = nseq->first_free - 1;
  198. int mid;
  199. while (low <= high) {
  200. mid = (low + high) / 2;
  201. if (instance < sseqs[mid].lower)
  202. high = mid - 1;
  203. else if (instance > sseqs[mid].upper)
  204. low = mid + 1;
  205. else
  206. return mid;
  207. }
  208. return low;
  209. }
  210. /**
  211. * tipc_nameseq_insert_publ -
  212. */
  213. static struct publication *tipc_nameseq_insert_publ(struct name_seq *nseq,
  214. u32 type, u32 lower, u32 upper,
  215. u32 scope, u32 node, u32 port, u32 key)
  216. {
  217. struct subscription *s;
  218. struct subscription *st;
  219. struct publication *publ;
  220. struct sub_seq *sseq;
  221. int created_subseq = 0;
  222. sseq = nameseq_find_subseq(nseq, lower);
  223. if (sseq) {
  224. /* Lower end overlaps existing entry => need an exact match */
  225. if ((sseq->lower != lower) || (sseq->upper != upper)) {
  226. warn("Cannot publish {%u,%u,%u}, overlap error\n",
  227. type, lower, upper);
  228. return NULL;
  229. }
  230. } else {
  231. u32 inspos;
  232. struct sub_seq *freesseq;
  233. /* Find where lower end should be inserted */
  234. inspos = nameseq_locate_subseq(nseq, lower);
  235. /* Fail if upper end overlaps into an existing entry */
  236. if ((inspos < nseq->first_free) &&
  237. (upper >= nseq->sseqs[inspos].lower)) {
  238. warn("Cannot publish {%u,%u,%u}, overlap error\n",
  239. type, lower, upper);
  240. return NULL;
  241. }
  242. /* Ensure there is space for new sub-sequence */
  243. if (nseq->first_free == nseq->alloc) {
  244. struct sub_seq *sseqs = tipc_subseq_alloc(nseq->alloc * 2);
  245. if (!sseqs) {
  246. warn("Cannot publish {%u,%u,%u}, no memory\n",
  247. type, lower, upper);
  248. return NULL;
  249. }
  250. memcpy(sseqs, nseq->sseqs,
  251. nseq->alloc * sizeof(struct sub_seq));
  252. kfree(nseq->sseqs);
  253. nseq->sseqs = sseqs;
  254. nseq->alloc *= 2;
  255. }
  256. /* Insert new sub-sequence */
  257. sseq = &nseq->sseqs[inspos];
  258. freesseq = &nseq->sseqs[nseq->first_free];
  259. memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof(*sseq));
  260. memset(sseq, 0, sizeof(*sseq));
  261. nseq->first_free++;
  262. sseq->lower = lower;
  263. sseq->upper = upper;
  264. created_subseq = 1;
  265. }
  266. /* Insert a publication: */
  267. publ = publ_create(type, lower, upper, scope, node, port, key);
  268. if (!publ)
  269. return NULL;
  270. sseq->zone_list_size++;
  271. if (!sseq->zone_list)
  272. sseq->zone_list = publ->zone_list_next = publ;
  273. else {
  274. publ->zone_list_next = sseq->zone_list->zone_list_next;
  275. sseq->zone_list->zone_list_next = publ;
  276. }
  277. if (in_own_cluster(node)) {
  278. sseq->cluster_list_size++;
  279. if (!sseq->cluster_list)
  280. sseq->cluster_list = publ->cluster_list_next = publ;
  281. else {
  282. publ->cluster_list_next =
  283. sseq->cluster_list->cluster_list_next;
  284. sseq->cluster_list->cluster_list_next = publ;
  285. }
  286. }
  287. if (node == tipc_own_addr) {
  288. sseq->node_list_size++;
  289. if (!sseq->node_list)
  290. sseq->node_list = publ->node_list_next = publ;
  291. else {
  292. publ->node_list_next = sseq->node_list->node_list_next;
  293. sseq->node_list->node_list_next = publ;
  294. }
  295. }
  296. /*
  297. * Any subscriptions waiting for notification?
  298. */
  299. list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
  300. tipc_subscr_report_overlap(s,
  301. publ->lower,
  302. publ->upper,
  303. TIPC_PUBLISHED,
  304. publ->ref,
  305. publ->node,
  306. created_subseq);
  307. }
  308. return publ;
  309. }
  310. /**
  311. * tipc_nameseq_remove_publ -
  312. *
  313. * NOTE: There may be cases where TIPC is asked to remove a publication
  314. * that is not in the name table. For example, if another node issues a
  315. * publication for a name sequence that overlaps an existing name sequence
  316. * the publication will not be recorded, which means the publication won't
  317. * be found when the name sequence is later withdrawn by that node.
  318. * A failed withdraw request simply returns a failure indication and lets the
  319. * caller issue any error or warning messages associated with such a problem.
  320. */
  321. static struct publication *tipc_nameseq_remove_publ(struct name_seq *nseq, u32 inst,
  322. u32 node, u32 ref, u32 key)
  323. {
  324. struct publication *publ;
  325. struct publication *curr;
  326. struct publication *prev;
  327. struct sub_seq *sseq = nameseq_find_subseq(nseq, inst);
  328. struct sub_seq *free;
  329. struct subscription *s, *st;
  330. int removed_subseq = 0;
  331. if (!sseq)
  332. return NULL;
  333. /* Remove publication from zone scope list */
  334. prev = sseq->zone_list;
  335. publ = sseq->zone_list->zone_list_next;
  336. while ((publ->key != key) || (publ->ref != ref) ||
  337. (publ->node && (publ->node != node))) {
  338. prev = publ;
  339. publ = publ->zone_list_next;
  340. if (prev == sseq->zone_list) {
  341. /* Prevent endless loop if publication not found */
  342. return NULL;
  343. }
  344. }
  345. if (publ != sseq->zone_list)
  346. prev->zone_list_next = publ->zone_list_next;
  347. else if (publ->zone_list_next != publ) {
  348. prev->zone_list_next = publ->zone_list_next;
  349. sseq->zone_list = publ->zone_list_next;
  350. } else {
  351. sseq->zone_list = NULL;
  352. }
  353. sseq->zone_list_size--;
  354. /* Remove publication from cluster scope list, if present */
  355. if (in_own_cluster(node)) {
  356. prev = sseq->cluster_list;
  357. curr = sseq->cluster_list->cluster_list_next;
  358. while (curr != publ) {
  359. prev = curr;
  360. curr = curr->cluster_list_next;
  361. if (prev == sseq->cluster_list) {
  362. /* Prevent endless loop for malformed list */
  363. err("Unable to de-list cluster publication\n"
  364. "{%u%u}, node=0x%x, ref=%u, key=%u)\n",
  365. publ->type, publ->lower, publ->node,
  366. publ->ref, publ->key);
  367. goto end_cluster;
  368. }
  369. }
  370. if (publ != sseq->cluster_list)
  371. prev->cluster_list_next = publ->cluster_list_next;
  372. else if (publ->cluster_list_next != publ) {
  373. prev->cluster_list_next = publ->cluster_list_next;
  374. sseq->cluster_list = publ->cluster_list_next;
  375. } else {
  376. sseq->cluster_list = NULL;
  377. }
  378. sseq->cluster_list_size--;
  379. }
  380. end_cluster:
  381. /* Remove publication from node scope list, if present */
  382. if (node == tipc_own_addr) {
  383. prev = sseq->node_list;
  384. curr = sseq->node_list->node_list_next;
  385. while (curr != publ) {
  386. prev = curr;
  387. curr = curr->node_list_next;
  388. if (prev == sseq->node_list) {
  389. /* Prevent endless loop for malformed list */
  390. err("Unable to de-list node publication\n"
  391. "{%u%u}, node=0x%x, ref=%u, key=%u)\n",
  392. publ->type, publ->lower, publ->node,
  393. publ->ref, publ->key);
  394. goto end_node;
  395. }
  396. }
  397. if (publ != sseq->node_list)
  398. prev->node_list_next = publ->node_list_next;
  399. else if (publ->node_list_next != publ) {
  400. prev->node_list_next = publ->node_list_next;
  401. sseq->node_list = publ->node_list_next;
  402. } else {
  403. sseq->node_list = NULL;
  404. }
  405. sseq->node_list_size--;
  406. }
  407. end_node:
  408. /* Contract subseq list if no more publications for that subseq */
  409. if (!sseq->zone_list) {
  410. free = &nseq->sseqs[nseq->first_free--];
  411. memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq));
  412. removed_subseq = 1;
  413. }
  414. /* Notify any waiting subscriptions */
  415. list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
  416. tipc_subscr_report_overlap(s,
  417. publ->lower,
  418. publ->upper,
  419. TIPC_WITHDRAWN,
  420. publ->ref,
  421. publ->node,
  422. removed_subseq);
  423. }
  424. return publ;
  425. }
  426. /**
  427. * tipc_nameseq_subscribe: attach a subscription, and issue
  428. * the prescribed number of events if there is any sub-
  429. * sequence overlapping with the requested sequence
  430. */
  431. static void tipc_nameseq_subscribe(struct name_seq *nseq, struct subscription *s)
  432. {
  433. struct sub_seq *sseq = nseq->sseqs;
  434. list_add(&s->nameseq_list, &nseq->subscriptions);
  435. if (!sseq)
  436. return;
  437. while (sseq != &nseq->sseqs[nseq->first_free]) {
  438. struct publication *zl = sseq->zone_list;
  439. if (zl && tipc_subscr_overlap(s, sseq->lower, sseq->upper)) {
  440. struct publication *crs = zl;
  441. int must_report = 1;
  442. do {
  443. tipc_subscr_report_overlap(s,
  444. sseq->lower,
  445. sseq->upper,
  446. TIPC_PUBLISHED,
  447. crs->ref,
  448. crs->node,
  449. must_report);
  450. must_report = 0;
  451. crs = crs->zone_list_next;
  452. } while (crs != zl);
  453. }
  454. sseq++;
  455. }
  456. }
  457. static struct name_seq *nametbl_find_seq(u32 type)
  458. {
  459. struct hlist_head *seq_head;
  460. struct hlist_node *seq_node;
  461. struct name_seq *ns;
  462. seq_head = &table.types[hash(type)];
  463. hlist_for_each_entry(ns, seq_node, seq_head, ns_list) {
  464. if (ns->type == type)
  465. return ns;
  466. }
  467. return NULL;
  468. };
  469. struct publication *tipc_nametbl_insert_publ(u32 type, u32 lower, u32 upper,
  470. u32 scope, u32 node, u32 port, u32 key)
  471. {
  472. struct name_seq *seq = nametbl_find_seq(type);
  473. if (lower > upper) {
  474. warn("Failed to publish illegal {%u,%u,%u}\n",
  475. type, lower, upper);
  476. return NULL;
  477. }
  478. if (!seq)
  479. seq = tipc_nameseq_create(type, &table.types[hash(type)]);
  480. if (!seq)
  481. return NULL;
  482. return tipc_nameseq_insert_publ(seq, type, lower, upper,
  483. scope, node, port, key);
  484. }
  485. struct publication *tipc_nametbl_remove_publ(u32 type, u32 lower,
  486. u32 node, u32 ref, u32 key)
  487. {
  488. struct publication *publ;
  489. struct name_seq *seq = nametbl_find_seq(type);
  490. if (!seq)
  491. return NULL;
  492. publ = tipc_nameseq_remove_publ(seq, lower, node, ref, key);
  493. if (!seq->first_free && list_empty(&seq->subscriptions)) {
  494. hlist_del_init(&seq->ns_list);
  495. kfree(seq->sseqs);
  496. kfree(seq);
  497. }
  498. return publ;
  499. }
  500. /*
  501. * tipc_nametbl_translate - translate name to port id
  502. *
  503. * Note: on entry 'destnode' is the search domain used during translation;
  504. * on exit it passes back the node address of the matching port (if any)
  505. */
  506. u32 tipc_nametbl_translate(u32 type, u32 instance, u32 *destnode)
  507. {
  508. struct sub_seq *sseq;
  509. struct publication *publ = NULL;
  510. struct name_seq *seq;
  511. u32 ref;
  512. if (!tipc_in_scope(*destnode, tipc_own_addr))
  513. return 0;
  514. read_lock_bh(&tipc_nametbl_lock);
  515. seq = nametbl_find_seq(type);
  516. if (unlikely(!seq))
  517. goto not_found;
  518. sseq = nameseq_find_subseq(seq, instance);
  519. if (unlikely(!sseq))
  520. goto not_found;
  521. spin_lock_bh(&seq->lock);
  522. /* Closest-First Algorithm: */
  523. if (likely(!*destnode)) {
  524. publ = sseq->node_list;
  525. if (publ) {
  526. sseq->node_list = publ->node_list_next;
  527. found:
  528. ref = publ->ref;
  529. *destnode = publ->node;
  530. spin_unlock_bh(&seq->lock);
  531. read_unlock_bh(&tipc_nametbl_lock);
  532. return ref;
  533. }
  534. publ = sseq->cluster_list;
  535. if (publ) {
  536. sseq->cluster_list = publ->cluster_list_next;
  537. goto found;
  538. }
  539. publ = sseq->zone_list;
  540. if (publ) {
  541. sseq->zone_list = publ->zone_list_next;
  542. goto found;
  543. }
  544. }
  545. /* Round-Robin Algorithm: */
  546. else if (*destnode == tipc_own_addr) {
  547. publ = sseq->node_list;
  548. if (publ) {
  549. sseq->node_list = publ->node_list_next;
  550. goto found;
  551. }
  552. } else if (in_own_cluster(*destnode)) {
  553. publ = sseq->cluster_list;
  554. if (publ) {
  555. sseq->cluster_list = publ->cluster_list_next;
  556. goto found;
  557. }
  558. } else {
  559. publ = sseq->zone_list;
  560. if (publ) {
  561. sseq->zone_list = publ->zone_list_next;
  562. goto found;
  563. }
  564. }
  565. spin_unlock_bh(&seq->lock);
  566. not_found:
  567. read_unlock_bh(&tipc_nametbl_lock);
  568. return 0;
  569. }
  570. /**
  571. * tipc_nametbl_mc_translate - find multicast destinations
  572. *
  573. * Creates list of all local ports that overlap the given multicast address;
  574. * also determines if any off-node ports overlap.
  575. *
  576. * Note: Publications with a scope narrower than 'limit' are ignored.
  577. * (i.e. local node-scope publications mustn't receive messages arriving
  578. * from another node, even if the multcast link brought it here)
  579. *
  580. * Returns non-zero if any off-node ports overlap
  581. */
  582. int tipc_nametbl_mc_translate(u32 type, u32 lower, u32 upper, u32 limit,
  583. struct port_list *dports)
  584. {
  585. struct name_seq *seq;
  586. struct sub_seq *sseq;
  587. struct sub_seq *sseq_stop;
  588. int res = 0;
  589. read_lock_bh(&tipc_nametbl_lock);
  590. seq = nametbl_find_seq(type);
  591. if (!seq)
  592. goto exit;
  593. spin_lock_bh(&seq->lock);
  594. sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
  595. sseq_stop = seq->sseqs + seq->first_free;
  596. for (; sseq != sseq_stop; sseq++) {
  597. struct publication *publ;
  598. if (sseq->lower > upper)
  599. break;
  600. publ = sseq->node_list;
  601. if (publ) {
  602. do {
  603. if (publ->scope <= limit)
  604. tipc_port_list_add(dports, publ->ref);
  605. publ = publ->node_list_next;
  606. } while (publ != sseq->node_list);
  607. }
  608. if (sseq->cluster_list_size != sseq->node_list_size)
  609. res = 1;
  610. }
  611. spin_unlock_bh(&seq->lock);
  612. exit:
  613. read_unlock_bh(&tipc_nametbl_lock);
  614. return res;
  615. }
  616. /**
  617. * tipc_nametbl_publish_rsv - publish port name using a reserved name type
  618. */
  619. int tipc_nametbl_publish_rsv(u32 ref, unsigned int scope,
  620. struct tipc_name_seq const *seq)
  621. {
  622. int res;
  623. atomic_inc(&rsv_publ_ok);
  624. res = tipc_publish(ref, scope, seq);
  625. atomic_dec(&rsv_publ_ok);
  626. return res;
  627. }
  628. /**
  629. * tipc_nametbl_publish - add name publication to network name tables
  630. */
  631. struct publication *tipc_nametbl_publish(u32 type, u32 lower, u32 upper,
  632. u32 scope, u32 port_ref, u32 key)
  633. {
  634. struct publication *publ;
  635. if (table.local_publ_count >= tipc_max_publications) {
  636. warn("Publication failed, local publication limit reached (%u)\n",
  637. tipc_max_publications);
  638. return NULL;
  639. }
  640. if ((type < TIPC_RESERVED_TYPES) && !atomic_read(&rsv_publ_ok)) {
  641. warn("Publication failed, reserved name {%u,%u,%u}\n",
  642. type, lower, upper);
  643. return NULL;
  644. }
  645. write_lock_bh(&tipc_nametbl_lock);
  646. table.local_publ_count++;
  647. publ = tipc_nametbl_insert_publ(type, lower, upper, scope,
  648. tipc_own_addr, port_ref, key);
  649. if (publ && (scope != TIPC_NODE_SCOPE))
  650. tipc_named_publish(publ);
  651. write_unlock_bh(&tipc_nametbl_lock);
  652. return publ;
  653. }
  654. /**
  655. * tipc_nametbl_withdraw - withdraw name publication from network name tables
  656. */
  657. int tipc_nametbl_withdraw(u32 type, u32 lower, u32 ref, u32 key)
  658. {
  659. struct publication *publ;
  660. write_lock_bh(&tipc_nametbl_lock);
  661. publ = tipc_nametbl_remove_publ(type, lower, tipc_own_addr, ref, key);
  662. if (likely(publ)) {
  663. table.local_publ_count--;
  664. if (publ->scope != TIPC_NODE_SCOPE)
  665. tipc_named_withdraw(publ);
  666. write_unlock_bh(&tipc_nametbl_lock);
  667. list_del_init(&publ->pport_list);
  668. kfree(publ);
  669. return 1;
  670. }
  671. write_unlock_bh(&tipc_nametbl_lock);
  672. err("Unable to remove local publication\n"
  673. "(type=%u, lower=%u, ref=%u, key=%u)\n",
  674. type, lower, ref, key);
  675. return 0;
  676. }
  677. /**
  678. * tipc_nametbl_subscribe - add a subscription object to the name table
  679. */
  680. void tipc_nametbl_subscribe(struct subscription *s)
  681. {
  682. u32 type = s->seq.type;
  683. struct name_seq *seq;
  684. write_lock_bh(&tipc_nametbl_lock);
  685. seq = nametbl_find_seq(type);
  686. if (!seq)
  687. seq = tipc_nameseq_create(type, &table.types[hash(type)]);
  688. if (seq) {
  689. spin_lock_bh(&seq->lock);
  690. tipc_nameseq_subscribe(seq, s);
  691. spin_unlock_bh(&seq->lock);
  692. } else {
  693. warn("Failed to create subscription for {%u,%u,%u}\n",
  694. s->seq.type, s->seq.lower, s->seq.upper);
  695. }
  696. write_unlock_bh(&tipc_nametbl_lock);
  697. }
  698. /**
  699. * tipc_nametbl_unsubscribe - remove a subscription object from name table
  700. */
  701. void tipc_nametbl_unsubscribe(struct subscription *s)
  702. {
  703. struct name_seq *seq;
  704. write_lock_bh(&tipc_nametbl_lock);
  705. seq = nametbl_find_seq(s->seq.type);
  706. if (seq != NULL) {
  707. spin_lock_bh(&seq->lock);
  708. list_del_init(&s->nameseq_list);
  709. spin_unlock_bh(&seq->lock);
  710. if ((seq->first_free == 0) && list_empty(&seq->subscriptions)) {
  711. hlist_del_init(&seq->ns_list);
  712. kfree(seq->sseqs);
  713. kfree(seq);
  714. }
  715. }
  716. write_unlock_bh(&tipc_nametbl_lock);
  717. }
  718. /**
  719. * subseq_list: print specified sub-sequence contents into the given buffer
  720. */
  721. static void subseq_list(struct sub_seq *sseq, struct print_buf *buf, u32 depth,
  722. u32 index)
  723. {
  724. char portIdStr[27];
  725. const char *scope_str[] = {"", " zone", " cluster", " node"};
  726. struct publication *publ = sseq->zone_list;
  727. tipc_printf(buf, "%-10u %-10u ", sseq->lower, sseq->upper);
  728. if (depth == 2 || !publ) {
  729. tipc_printf(buf, "\n");
  730. return;
  731. }
  732. do {
  733. sprintf(portIdStr, "<%u.%u.%u:%u>",
  734. tipc_zone(publ->node), tipc_cluster(publ->node),
  735. tipc_node(publ->node), publ->ref);
  736. tipc_printf(buf, "%-26s ", portIdStr);
  737. if (depth > 3) {
  738. tipc_printf(buf, "%-10u %s", publ->key,
  739. scope_str[publ->scope]);
  740. }
  741. publ = publ->zone_list_next;
  742. if (publ == sseq->zone_list)
  743. break;
  744. tipc_printf(buf, "\n%33s", " ");
  745. } while (1);
  746. tipc_printf(buf, "\n");
  747. }
  748. /**
  749. * nameseq_list: print specified name sequence contents into the given buffer
  750. */
  751. static void nameseq_list(struct name_seq *seq, struct print_buf *buf, u32 depth,
  752. u32 type, u32 lowbound, u32 upbound, u32 index)
  753. {
  754. struct sub_seq *sseq;
  755. char typearea[11];
  756. if (seq->first_free == 0)
  757. return;
  758. sprintf(typearea, "%-10u", seq->type);
  759. if (depth == 1) {
  760. tipc_printf(buf, "%s\n", typearea);
  761. return;
  762. }
  763. for (sseq = seq->sseqs; sseq != &seq->sseqs[seq->first_free]; sseq++) {
  764. if ((lowbound <= sseq->upper) && (upbound >= sseq->lower)) {
  765. tipc_printf(buf, "%s ", typearea);
  766. spin_lock_bh(&seq->lock);
  767. subseq_list(sseq, buf, depth, index);
  768. spin_unlock_bh(&seq->lock);
  769. sprintf(typearea, "%10s", " ");
  770. }
  771. }
  772. }
  773. /**
  774. * nametbl_header - print name table header into the given buffer
  775. */
  776. static void nametbl_header(struct print_buf *buf, u32 depth)
  777. {
  778. const char *header[] = {
  779. "Type ",
  780. "Lower Upper ",
  781. "Port Identity ",
  782. "Publication Scope"
  783. };
  784. int i;
  785. if (depth > 4)
  786. depth = 4;
  787. for (i = 0; i < depth; i++)
  788. tipc_printf(buf, header[i]);
  789. tipc_printf(buf, "\n");
  790. }
  791. /**
  792. * nametbl_list - print specified name table contents into the given buffer
  793. */
  794. static void nametbl_list(struct print_buf *buf, u32 depth_info,
  795. u32 type, u32 lowbound, u32 upbound)
  796. {
  797. struct hlist_head *seq_head;
  798. struct hlist_node *seq_node;
  799. struct name_seq *seq;
  800. int all_types;
  801. u32 depth;
  802. u32 i;
  803. all_types = (depth_info & TIPC_NTQ_ALLTYPES);
  804. depth = (depth_info & ~TIPC_NTQ_ALLTYPES);
  805. if (depth == 0)
  806. return;
  807. if (all_types) {
  808. /* display all entries in name table to specified depth */
  809. nametbl_header(buf, depth);
  810. lowbound = 0;
  811. upbound = ~0;
  812. for (i = 0; i < tipc_nametbl_size; i++) {
  813. seq_head = &table.types[i];
  814. hlist_for_each_entry(seq, seq_node, seq_head, ns_list) {
  815. nameseq_list(seq, buf, depth, seq->type,
  816. lowbound, upbound, i);
  817. }
  818. }
  819. } else {
  820. /* display only the sequence that matches the specified type */
  821. if (upbound < lowbound) {
  822. tipc_printf(buf, "invalid name sequence specified\n");
  823. return;
  824. }
  825. nametbl_header(buf, depth);
  826. i = hash(type);
  827. seq_head = &table.types[i];
  828. hlist_for_each_entry(seq, seq_node, seq_head, ns_list) {
  829. if (seq->type == type) {
  830. nameseq_list(seq, buf, depth, type,
  831. lowbound, upbound, i);
  832. break;
  833. }
  834. }
  835. }
  836. }
  837. #define MAX_NAME_TBL_QUERY 32768
  838. struct sk_buff *tipc_nametbl_get(const void *req_tlv_area, int req_tlv_space)
  839. {
  840. struct sk_buff *buf;
  841. struct tipc_name_table_query *argv;
  842. struct tlv_desc *rep_tlv;
  843. struct print_buf b;
  844. int str_len;
  845. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NAME_TBL_QUERY))
  846. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  847. buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_NAME_TBL_QUERY));
  848. if (!buf)
  849. return NULL;
  850. rep_tlv = (struct tlv_desc *)buf->data;
  851. tipc_printbuf_init(&b, TLV_DATA(rep_tlv), MAX_NAME_TBL_QUERY);
  852. argv = (struct tipc_name_table_query *)TLV_DATA(req_tlv_area);
  853. read_lock_bh(&tipc_nametbl_lock);
  854. nametbl_list(&b, ntohl(argv->depth), ntohl(argv->type),
  855. ntohl(argv->lowbound), ntohl(argv->upbound));
  856. read_unlock_bh(&tipc_nametbl_lock);
  857. str_len = tipc_printbuf_validate(&b);
  858. skb_put(buf, TLV_SPACE(str_len));
  859. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  860. return buf;
  861. }
  862. int tipc_nametbl_init(void)
  863. {
  864. table.types = kcalloc(tipc_nametbl_size, sizeof(struct hlist_head),
  865. GFP_ATOMIC);
  866. if (!table.types)
  867. return -ENOMEM;
  868. table.local_publ_count = 0;
  869. return 0;
  870. }
  871. void tipc_nametbl_stop(void)
  872. {
  873. u32 i;
  874. if (!table.types)
  875. return;
  876. /* Verify name table is empty, then release it */
  877. write_lock_bh(&tipc_nametbl_lock);
  878. for (i = 0; i < tipc_nametbl_size; i++) {
  879. if (!hlist_empty(&table.types[i]))
  880. err("tipc_nametbl_stop(): hash chain %u is non-null\n", i);
  881. }
  882. kfree(table.types);
  883. table.types = NULL;
  884. write_unlock_bh(&tipc_nametbl_lock);
  885. }