name_table.c 27 KB

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