name_table.c 28 KB

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