name_table.c 28 KB

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