name_table.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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. }
  652. write_unlock_bh(&tipc_nametbl_lock);
  653. return publ;
  654. }
  655. /**
  656. * tipc_nametbl_withdraw - withdraw name publication from network name tables
  657. */
  658. int tipc_nametbl_withdraw(u32 type, u32 lower, u32 ref, u32 key)
  659. {
  660. struct publication *publ;
  661. write_lock_bh(&tipc_nametbl_lock);
  662. publ = tipc_nametbl_remove_publ(type, lower, tipc_own_addr, ref, key);
  663. if (likely(publ)) {
  664. table.local_publ_count--;
  665. if (publ->scope != TIPC_NODE_SCOPE)
  666. tipc_named_withdraw(publ);
  667. write_unlock_bh(&tipc_nametbl_lock);
  668. list_del_init(&publ->pport_list);
  669. kfree(publ);
  670. return 1;
  671. }
  672. write_unlock_bh(&tipc_nametbl_lock);
  673. err("Unable to remove local publication\n"
  674. "(type=%u, lower=%u, ref=%u, key=%u)\n",
  675. type, lower, ref, key);
  676. return 0;
  677. }
  678. /**
  679. * tipc_nametbl_subscribe - add a subscription object to the name table
  680. */
  681. void tipc_nametbl_subscribe(struct subscription *s)
  682. {
  683. u32 type = s->seq.type;
  684. struct name_seq *seq;
  685. write_lock_bh(&tipc_nametbl_lock);
  686. seq = nametbl_find_seq(type);
  687. if (!seq) {
  688. seq = tipc_nameseq_create(type, &table.types[hash(type)]);
  689. }
  690. if (seq) {
  691. spin_lock_bh(&seq->lock);
  692. tipc_nameseq_subscribe(seq, s);
  693. spin_unlock_bh(&seq->lock);
  694. } else {
  695. warn("Failed to create subscription for {%u,%u,%u}\n",
  696. s->seq.type, s->seq.lower, s->seq.upper);
  697. }
  698. write_unlock_bh(&tipc_nametbl_lock);
  699. }
  700. /**
  701. * tipc_nametbl_unsubscribe - remove a subscription object from name table
  702. */
  703. void tipc_nametbl_unsubscribe(struct subscription *s)
  704. {
  705. struct name_seq *seq;
  706. write_lock_bh(&tipc_nametbl_lock);
  707. seq = nametbl_find_seq(s->seq.type);
  708. if (seq != NULL) {
  709. spin_lock_bh(&seq->lock);
  710. list_del_init(&s->nameseq_list);
  711. spin_unlock_bh(&seq->lock);
  712. if ((seq->first_free == 0) && list_empty(&seq->subscriptions)) {
  713. hlist_del_init(&seq->ns_list);
  714. kfree(seq->sseqs);
  715. kfree(seq);
  716. }
  717. }
  718. write_unlock_bh(&tipc_nametbl_lock);
  719. }
  720. /**
  721. * subseq_list: print specified sub-sequence contents into the given buffer
  722. */
  723. static void subseq_list(struct sub_seq *sseq, struct print_buf *buf, u32 depth,
  724. u32 index)
  725. {
  726. char portIdStr[27];
  727. const char *scope_str[] = {"", " zone", " cluster", " node"};
  728. struct publication *publ = sseq->zone_list;
  729. tipc_printf(buf, "%-10u %-10u ", sseq->lower, sseq->upper);
  730. if (depth == 2 || !publ) {
  731. tipc_printf(buf, "\n");
  732. return;
  733. }
  734. do {
  735. sprintf(portIdStr, "<%u.%u.%u:%u>",
  736. tipc_zone(publ->node), tipc_cluster(publ->node),
  737. tipc_node(publ->node), publ->ref);
  738. tipc_printf(buf, "%-26s ", portIdStr);
  739. if (depth > 3) {
  740. tipc_printf(buf, "%-10u %s", publ->key,
  741. scope_str[publ->scope]);
  742. }
  743. publ = publ->zone_list_next;
  744. if (publ == sseq->zone_list)
  745. break;
  746. tipc_printf(buf, "\n%33s", " ");
  747. } while (1);
  748. tipc_printf(buf, "\n");
  749. }
  750. /**
  751. * nameseq_list: print specified name sequence contents into the given buffer
  752. */
  753. static void nameseq_list(struct name_seq *seq, struct print_buf *buf, u32 depth,
  754. u32 type, u32 lowbound, u32 upbound, u32 index)
  755. {
  756. struct sub_seq *sseq;
  757. char typearea[11];
  758. if (seq->first_free == 0)
  759. return;
  760. sprintf(typearea, "%-10u", seq->type);
  761. if (depth == 1) {
  762. tipc_printf(buf, "%s\n", typearea);
  763. return;
  764. }
  765. for (sseq = seq->sseqs; sseq != &seq->sseqs[seq->first_free]; sseq++) {
  766. if ((lowbound <= sseq->upper) && (upbound >= sseq->lower)) {
  767. tipc_printf(buf, "%s ", typearea);
  768. spin_lock_bh(&seq->lock);
  769. subseq_list(sseq, buf, depth, index);
  770. spin_unlock_bh(&seq->lock);
  771. sprintf(typearea, "%10s", " ");
  772. }
  773. }
  774. }
  775. /**
  776. * nametbl_header - print name table header into the given buffer
  777. */
  778. static void nametbl_header(struct print_buf *buf, u32 depth)
  779. {
  780. const char *header[] = {
  781. "Type ",
  782. "Lower Upper ",
  783. "Port Identity ",
  784. "Publication Scope"
  785. };
  786. int i;
  787. if (depth > 4)
  788. depth = 4;
  789. for (i = 0; i < depth; i++)
  790. tipc_printf(buf, header[i]);
  791. tipc_printf(buf, "\n");
  792. }
  793. /**
  794. * nametbl_list - print specified name table contents into the given buffer
  795. */
  796. static void nametbl_list(struct print_buf *buf, u32 depth_info,
  797. u32 type, u32 lowbound, u32 upbound)
  798. {
  799. struct hlist_head *seq_head;
  800. struct hlist_node *seq_node;
  801. struct name_seq *seq;
  802. int all_types;
  803. u32 depth;
  804. u32 i;
  805. all_types = (depth_info & TIPC_NTQ_ALLTYPES);
  806. depth = (depth_info & ~TIPC_NTQ_ALLTYPES);
  807. if (depth == 0)
  808. return;
  809. if (all_types) {
  810. /* display all entries in name table to specified depth */
  811. nametbl_header(buf, depth);
  812. lowbound = 0;
  813. upbound = ~0;
  814. for (i = 0; i < tipc_nametbl_size; i++) {
  815. seq_head = &table.types[i];
  816. hlist_for_each_entry(seq, seq_node, seq_head, ns_list) {
  817. nameseq_list(seq, buf, depth, seq->type,
  818. lowbound, upbound, i);
  819. }
  820. }
  821. } else {
  822. /* display only the sequence that matches the specified type */
  823. if (upbound < lowbound) {
  824. tipc_printf(buf, "invalid name sequence specified\n");
  825. return;
  826. }
  827. nametbl_header(buf, depth);
  828. i = hash(type);
  829. seq_head = &table.types[i];
  830. hlist_for_each_entry(seq, seq_node, seq_head, ns_list) {
  831. if (seq->type == type) {
  832. nameseq_list(seq, buf, depth, type,
  833. lowbound, upbound, i);
  834. break;
  835. }
  836. }
  837. }
  838. }
  839. #define MAX_NAME_TBL_QUERY 32768
  840. struct sk_buff *tipc_nametbl_get(const void *req_tlv_area, int req_tlv_space)
  841. {
  842. struct sk_buff *buf;
  843. struct tipc_name_table_query *argv;
  844. struct tlv_desc *rep_tlv;
  845. struct print_buf b;
  846. int str_len;
  847. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NAME_TBL_QUERY))
  848. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  849. buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_NAME_TBL_QUERY));
  850. if (!buf)
  851. return NULL;
  852. rep_tlv = (struct tlv_desc *)buf->data;
  853. tipc_printbuf_init(&b, TLV_DATA(rep_tlv), MAX_NAME_TBL_QUERY);
  854. argv = (struct tipc_name_table_query *)TLV_DATA(req_tlv_area);
  855. read_lock_bh(&tipc_nametbl_lock);
  856. nametbl_list(&b, ntohl(argv->depth), ntohl(argv->type),
  857. ntohl(argv->lowbound), ntohl(argv->upbound));
  858. read_unlock_bh(&tipc_nametbl_lock);
  859. str_len = tipc_printbuf_validate(&b);
  860. skb_put(buf, TLV_SPACE(str_len));
  861. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  862. return buf;
  863. }
  864. int tipc_nametbl_init(void)
  865. {
  866. table.types = kcalloc(tipc_nametbl_size, sizeof(struct hlist_head),
  867. GFP_ATOMIC);
  868. if (!table.types)
  869. return -ENOMEM;
  870. table.local_publ_count = 0;
  871. return 0;
  872. }
  873. void tipc_nametbl_stop(void)
  874. {
  875. u32 i;
  876. if (!table.types)
  877. return;
  878. /* Verify name table is empty, then release it */
  879. write_lock_bh(&tipc_nametbl_lock);
  880. for (i = 0; i < tipc_nametbl_size; i++) {
  881. if (!hlist_empty(&table.types[i]))
  882. err("tipc_nametbl_stop(): hash chain %u is non-null\n", i);
  883. }
  884. kfree(table.types);
  885. table.types = NULL;
  886. write_unlock_bh(&tipc_nametbl_lock);
  887. }