name_table.c 25 KB

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