name_table.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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: upper bound of highest sub-sequence + 1
  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. rwlock_t tipc_nametbl_lock = RW_LOCK_UNLOCKED;
  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("Memory squeeze; failed to create publication\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("Memory squeeze; failed to create name sequence\n");
  149. kfree(nseq);
  150. kfree(sseq);
  151. return NULL;
  152. }
  153. memset(nseq, 0, sizeof(*nseq));
  154. nseq->lock = SPIN_LOCK_UNLOCKED;
  155. nseq->type = type;
  156. nseq->sseqs = sseq;
  157. dbg("tipc_nameseq_create() nseq = %x type %u, ssseqs %x, 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. assert(nseq->first_free <= nseq->alloc);
  227. sseq = nameseq_find_subseq(nseq, lower);
  228. dbg("nameseq_ins: for seq %x,<%u,%u>, found sseq %x\n",
  229. nseq, type, lower, sseq);
  230. if (sseq) {
  231. /* Lower end overlaps existing entry => need an exact match */
  232. if ((sseq->lower != lower) || (sseq->upper != upper)) {
  233. warn("Overlapping publ <%u,%u,%u>\n", 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("Overlapping publ <%u,%u,%u>\n", type, lower, upper);
  245. return NULL;
  246. }
  247. /* Ensure there is space for new sub-sequence */
  248. if (nseq->first_free == nseq->alloc) {
  249. struct sub_seq *sseqs = tipc_subseq_alloc(nseq->alloc * 2);
  250. if (!sseqs) {
  251. warn("Memory squeeze; failed to create sub-sequence\n");
  252. return NULL;
  253. }
  254. dbg("Allocated %u more sseqs\n", nseq->alloc);
  255. memcpy(sseqs, nseq->sseqs,
  256. nseq->alloc * sizeof(struct sub_seq));
  257. kfree(nseq->sseqs);
  258. nseq->sseqs = sseqs;
  259. nseq->alloc *= 2;
  260. }
  261. dbg("Have %u sseqs for type %u\n", nseq->alloc, type);
  262. /* Insert new sub-sequence */
  263. dbg("ins in pos %u, ff = %u\n", inspos, nseq->first_free);
  264. sseq = &nseq->sseqs[inspos];
  265. freesseq = &nseq->sseqs[nseq->first_free];
  266. memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof (*sseq));
  267. memset(sseq, 0, sizeof (*sseq));
  268. nseq->first_free++;
  269. sseq->lower = lower;
  270. sseq->upper = upper;
  271. created_subseq = 1;
  272. }
  273. dbg("inserting (%u %u %u) from %x:%u into sseq %x(%u,%u) of seq %x\n",
  274. type, lower, upper, node, port, sseq,
  275. sseq->lower, sseq->upper, nseq);
  276. /* Insert a publication: */
  277. publ = publ_create(type, lower, upper, scope, node, port, key);
  278. if (!publ)
  279. return NULL;
  280. dbg("inserting publ %x, node=%x publ->node=%x, subscr->node=%x\n",
  281. publ, node, publ->node, publ->subscr.node);
  282. if (!sseq->zone_list)
  283. sseq->zone_list = publ->zone_list_next = publ;
  284. else {
  285. publ->zone_list_next = sseq->zone_list->zone_list_next;
  286. sseq->zone_list->zone_list_next = publ;
  287. }
  288. if (in_own_cluster(node)) {
  289. if (!sseq->cluster_list)
  290. sseq->cluster_list = publ->cluster_list_next = publ;
  291. else {
  292. publ->cluster_list_next =
  293. sseq->cluster_list->cluster_list_next;
  294. sseq->cluster_list->cluster_list_next = publ;
  295. }
  296. }
  297. if (node == tipc_own_addr) {
  298. if (!sseq->node_list)
  299. sseq->node_list = publ->node_list_next = publ;
  300. else {
  301. publ->node_list_next = sseq->node_list->node_list_next;
  302. sseq->node_list->node_list_next = publ;
  303. }
  304. }
  305. /*
  306. * Any subscriptions waiting for notification?
  307. */
  308. list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
  309. dbg("calling report_overlap()\n");
  310. tipc_subscr_report_overlap(s,
  311. publ->lower,
  312. publ->upper,
  313. TIPC_PUBLISHED,
  314. publ->ref,
  315. publ->node,
  316. created_subseq);
  317. }
  318. return publ;
  319. }
  320. /**
  321. * tipc_nameseq_remove_publ -
  322. */
  323. static struct publication *tipc_nameseq_remove_publ(struct name_seq *nseq, u32 inst,
  324. u32 node, u32 ref, u32 key)
  325. {
  326. struct publication *publ;
  327. struct publication *prev;
  328. struct sub_seq *sseq = nameseq_find_subseq(nseq, inst);
  329. struct sub_seq *free;
  330. struct subscription *s, *st;
  331. int removed_subseq = 0;
  332. assert(nseq);
  333. if (!sseq) {
  334. int i;
  335. warn("Withdraw unknown <%u,%u>?\n", nseq->type, inst);
  336. assert(nseq->sseqs);
  337. dbg("Dumping subseqs %x for %x, alloc = %u,ff=%u\n",
  338. nseq->sseqs, nseq, nseq->alloc,
  339. nseq->first_free);
  340. for (i = 0; i < nseq->first_free; i++) {
  341. dbg("Subseq %u(%x): lower = %u,upper = %u\n",
  342. i, &nseq->sseqs[i], nseq->sseqs[i].lower,
  343. nseq->sseqs[i].upper);
  344. }
  345. return NULL;
  346. }
  347. dbg("nameseq_remove: seq: %x, sseq %x, <%u,%u> key %u\n",
  348. nseq, sseq, nseq->type, inst, key);
  349. prev = sseq->zone_list;
  350. publ = sseq->zone_list->zone_list_next;
  351. while ((publ->key != key) || (publ->ref != ref) ||
  352. (publ->node && (publ->node != node))) {
  353. prev = publ;
  354. publ = publ->zone_list_next;
  355. assert(prev != sseq->zone_list);
  356. }
  357. if (publ != sseq->zone_list)
  358. prev->zone_list_next = publ->zone_list_next;
  359. else if (publ->zone_list_next != publ) {
  360. prev->zone_list_next = publ->zone_list_next;
  361. sseq->zone_list = publ->zone_list_next;
  362. } else {
  363. sseq->zone_list = NULL;
  364. }
  365. if (in_own_cluster(node)) {
  366. prev = sseq->cluster_list;
  367. publ = sseq->cluster_list->cluster_list_next;
  368. while ((publ->key != key) || (publ->ref != ref) ||
  369. (publ->node && (publ->node != node))) {
  370. prev = publ;
  371. publ = publ->cluster_list_next;
  372. assert(prev != sseq->cluster_list);
  373. }
  374. if (publ != sseq->cluster_list)
  375. prev->cluster_list_next = publ->cluster_list_next;
  376. else if (publ->cluster_list_next != publ) {
  377. prev->cluster_list_next = publ->cluster_list_next;
  378. sseq->cluster_list = publ->cluster_list_next;
  379. } else {
  380. sseq->cluster_list = NULL;
  381. }
  382. }
  383. if (node == tipc_own_addr) {
  384. prev = sseq->node_list;
  385. publ = sseq->node_list->node_list_next;
  386. while ((publ->key != key) || (publ->ref != ref) ||
  387. (publ->node && (publ->node != node))) {
  388. prev = publ;
  389. publ = publ->node_list_next;
  390. assert(prev != sseq->node_list);
  391. }
  392. if (publ != sseq->node_list)
  393. prev->node_list_next = publ->node_list_next;
  394. else if (publ->node_list_next != publ) {
  395. prev->node_list_next = publ->node_list_next;
  396. sseq->node_list = publ->node_list_next;
  397. } else {
  398. sseq->node_list = NULL;
  399. }
  400. }
  401. assert(!publ->node || (publ->node == node));
  402. assert(publ->ref == ref);
  403. assert(publ->key == key);
  404. /*
  405. * Contract subseq list if no more publications:
  406. */
  407. if (!sseq->node_list && !sseq->cluster_list && !sseq->zone_list) {
  408. free = &nseq->sseqs[nseq->first_free--];
  409. memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof (*sseq));
  410. removed_subseq = 1;
  411. }
  412. /*
  413. * Any subscriptions waiting ?
  414. */
  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. 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. dbg("find_seq %u,(%u,0x%x) table = %p, hash[type] = %u\n",
  463. type, ntohl(type), type, table.types, hash(type));
  464. seq_head = &table.types[hash(type)];
  465. hlist_for_each_entry(ns, seq_node, seq_head, ns_list) {
  466. if (ns->type == type) {
  467. dbg("found %x\n", ns);
  468. return ns;
  469. }
  470. }
  471. return NULL;
  472. };
  473. struct publication *tipc_nametbl_insert_publ(u32 type, u32 lower, u32 upper,
  474. u32 scope, u32 node, u32 port, u32 key)
  475. {
  476. struct name_seq *seq = nametbl_find_seq(type);
  477. dbg("ins_publ: <%u,%x,%x> found %x\n", type, lower, upper, seq);
  478. if (lower > upper) {
  479. warn("Failed to publish illegal <%u,%u,%u>\n",
  480. type, lower, upper);
  481. return NULL;
  482. }
  483. dbg("Publishing <%u,%u,%u> from %x\n", type, lower, upper, node);
  484. if (!seq) {
  485. seq = tipc_nameseq_create(type, &table.types[hash(type)]);
  486. dbg("tipc_nametbl_insert_publ: created %x\n", seq);
  487. }
  488. if (!seq)
  489. return NULL;
  490. assert(seq->type == type);
  491. return tipc_nameseq_insert_publ(seq, type, lower, upper,
  492. scope, node, port, key);
  493. }
  494. struct publication *tipc_nametbl_remove_publ(u32 type, u32 lower,
  495. u32 node, u32 ref, u32 key)
  496. {
  497. struct publication *publ;
  498. struct name_seq *seq = nametbl_find_seq(type);
  499. if (!seq)
  500. return NULL;
  501. dbg("Withdrawing <%u,%u> from %x\n", type, lower, node);
  502. publ = tipc_nameseq_remove_publ(seq, lower, node, ref, key);
  503. if (!seq->first_free && list_empty(&seq->subscriptions)) {
  504. hlist_del_init(&seq->ns_list);
  505. kfree(seq->sseqs);
  506. kfree(seq);
  507. }
  508. return publ;
  509. }
  510. /*
  511. * tipc_nametbl_translate(): Translate tipc_name -> tipc_portid.
  512. * Very time-critical.
  513. *
  514. * Note: on entry 'destnode' is the search domain used during translation;
  515. * on exit it passes back the node address of the matching port (if any)
  516. */
  517. u32 tipc_nametbl_translate(u32 type, u32 instance, u32 *destnode)
  518. {
  519. struct sub_seq *sseq;
  520. struct publication *publ = NULL;
  521. struct name_seq *seq;
  522. u32 ref;
  523. if (!in_scope(*destnode, tipc_own_addr))
  524. return 0;
  525. read_lock_bh(&tipc_nametbl_lock);
  526. seq = nametbl_find_seq(type);
  527. if (unlikely(!seq))
  528. goto not_found;
  529. sseq = nameseq_find_subseq(seq, instance);
  530. if (unlikely(!sseq))
  531. goto not_found;
  532. spin_lock_bh(&seq->lock);
  533. /* Closest-First Algorithm: */
  534. if (likely(!*destnode)) {
  535. publ = sseq->node_list;
  536. if (publ) {
  537. sseq->node_list = publ->node_list_next;
  538. found:
  539. ref = publ->ref;
  540. *destnode = publ->node;
  541. spin_unlock_bh(&seq->lock);
  542. read_unlock_bh(&tipc_nametbl_lock);
  543. return ref;
  544. }
  545. publ = sseq->cluster_list;
  546. if (publ) {
  547. sseq->cluster_list = publ->cluster_list_next;
  548. goto found;
  549. }
  550. publ = sseq->zone_list;
  551. if (publ) {
  552. sseq->zone_list = publ->zone_list_next;
  553. goto found;
  554. }
  555. }
  556. /* Round-Robin Algorithm: */
  557. else if (*destnode == tipc_own_addr) {
  558. publ = sseq->node_list;
  559. if (publ) {
  560. sseq->node_list = publ->node_list_next;
  561. goto found;
  562. }
  563. } else if (in_own_cluster(*destnode)) {
  564. publ = sseq->cluster_list;
  565. if (publ) {
  566. sseq->cluster_list = publ->cluster_list_next;
  567. goto found;
  568. }
  569. } else {
  570. publ = sseq->zone_list;
  571. if (publ) {
  572. sseq->zone_list = publ->zone_list_next;
  573. goto found;
  574. }
  575. }
  576. spin_unlock_bh(&seq->lock);
  577. not_found:
  578. *destnode = 0;
  579. read_unlock_bh(&tipc_nametbl_lock);
  580. return 0;
  581. }
  582. /**
  583. * tipc_nametbl_mc_translate - find multicast destinations
  584. *
  585. * Creates list of all local ports that overlap the given multicast address;
  586. * also determines if any off-node ports overlap.
  587. *
  588. * Note: Publications with a scope narrower than 'limit' are ignored.
  589. * (i.e. local node-scope publications mustn't receive messages arriving
  590. * from another node, even if the multcast link brought it here)
  591. *
  592. * Returns non-zero if any off-node ports overlap
  593. */
  594. int tipc_nametbl_mc_translate(u32 type, u32 lower, u32 upper, u32 limit,
  595. struct port_list *dports)
  596. {
  597. struct name_seq *seq;
  598. struct sub_seq *sseq;
  599. struct sub_seq *sseq_stop;
  600. int res = 0;
  601. read_lock_bh(&tipc_nametbl_lock);
  602. seq = nametbl_find_seq(type);
  603. if (!seq)
  604. goto exit;
  605. spin_lock_bh(&seq->lock);
  606. sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
  607. sseq_stop = seq->sseqs + seq->first_free;
  608. for (; sseq != sseq_stop; sseq++) {
  609. struct publication *publ;
  610. if (sseq->lower > upper)
  611. break;
  612. publ = sseq->cluster_list;
  613. if (publ && (publ->scope <= limit))
  614. do {
  615. if (publ->node == tipc_own_addr)
  616. tipc_port_list_add(dports, publ->ref);
  617. else
  618. res = 1;
  619. publ = publ->cluster_list_next;
  620. } while (publ != sseq->cluster_list);
  621. }
  622. spin_unlock_bh(&seq->lock);
  623. exit:
  624. read_unlock_bh(&tipc_nametbl_lock);
  625. return res;
  626. }
  627. /**
  628. * tipc_nametbl_publish_rsv - publish port name using a reserved name type
  629. */
  630. int tipc_nametbl_publish_rsv(u32 ref, unsigned int scope,
  631. struct tipc_name_seq const *seq)
  632. {
  633. int res;
  634. atomic_inc(&rsv_publ_ok);
  635. res = tipc_publish(ref, scope, seq);
  636. atomic_dec(&rsv_publ_ok);
  637. return res;
  638. }
  639. /**
  640. * tipc_nametbl_publish - add name publication to network name tables
  641. */
  642. struct publication *tipc_nametbl_publish(u32 type, u32 lower, u32 upper,
  643. u32 scope, u32 port_ref, u32 key)
  644. {
  645. struct publication *publ;
  646. if (table.local_publ_count >= tipc_max_publications) {
  647. warn("Failed publish: max %u local publication\n",
  648. tipc_max_publications);
  649. return NULL;
  650. }
  651. if ((type < TIPC_RESERVED_TYPES) && !atomic_read(&rsv_publ_ok)) {
  652. warn("Failed to publish reserved name <%u,%u,%u>\n",
  653. type, lower, upper);
  654. return NULL;
  655. }
  656. write_lock_bh(&tipc_nametbl_lock);
  657. table.local_publ_count++;
  658. publ = tipc_nametbl_insert_publ(type, lower, upper, scope,
  659. tipc_own_addr, port_ref, key);
  660. if (publ && (scope != TIPC_NODE_SCOPE)) {
  661. tipc_named_publish(publ);
  662. }
  663. write_unlock_bh(&tipc_nametbl_lock);
  664. return publ;
  665. }
  666. /**
  667. * tipc_nametbl_withdraw - withdraw name publication from network name tables
  668. */
  669. int tipc_nametbl_withdraw(u32 type, u32 lower, u32 ref, u32 key)
  670. {
  671. struct publication *publ;
  672. dbg("tipc_nametbl_withdraw:<%d,%d,%d>\n", type, lower, key);
  673. write_lock_bh(&tipc_nametbl_lock);
  674. publ = tipc_nametbl_remove_publ(type, lower, tipc_own_addr, ref, key);
  675. if (publ) {
  676. table.local_publ_count--;
  677. if (publ->scope != TIPC_NODE_SCOPE)
  678. tipc_named_withdraw(publ);
  679. write_unlock_bh(&tipc_nametbl_lock);
  680. list_del_init(&publ->pport_list);
  681. kfree(publ);
  682. return 1;
  683. }
  684. write_unlock_bh(&tipc_nametbl_lock);
  685. return 0;
  686. }
  687. /**
  688. * tipc_nametbl_subscribe - add a subscription object to the name table
  689. */
  690. void
  691. tipc_nametbl_subscribe(struct subscription *s)
  692. {
  693. u32 type = s->seq.type;
  694. struct name_seq *seq;
  695. write_lock_bh(&tipc_nametbl_lock);
  696. seq = nametbl_find_seq(type);
  697. if (!seq) {
  698. seq = tipc_nameseq_create(type, &table.types[hash(type)]);
  699. }
  700. if (seq){
  701. spin_lock_bh(&seq->lock);
  702. dbg("tipc_nametbl_subscribe:found %x for <%u,%u,%u>\n",
  703. seq, type, s->seq.lower, s->seq.upper);
  704. assert(seq->type == type);
  705. tipc_nameseq_subscribe(seq, s);
  706. spin_unlock_bh(&seq->lock);
  707. }
  708. write_unlock_bh(&tipc_nametbl_lock);
  709. }
  710. /**
  711. * tipc_nametbl_unsubscribe - remove a subscription object from name table
  712. */
  713. void
  714. tipc_nametbl_unsubscribe(struct subscription *s)
  715. {
  716. struct name_seq *seq;
  717. write_lock_bh(&tipc_nametbl_lock);
  718. seq = nametbl_find_seq(s->seq.type);
  719. if (seq != NULL){
  720. spin_lock_bh(&seq->lock);
  721. list_del_init(&s->nameseq_list);
  722. spin_unlock_bh(&seq->lock);
  723. if ((seq->first_free == 0) && list_empty(&seq->subscriptions)) {
  724. hlist_del_init(&seq->ns_list);
  725. kfree(seq->sseqs);
  726. kfree(seq);
  727. }
  728. }
  729. write_unlock_bh(&tipc_nametbl_lock);
  730. }
  731. /**
  732. * subseq_list: print specified sub-sequence contents into the given buffer
  733. */
  734. static void subseq_list(struct sub_seq *sseq, struct print_buf *buf, u32 depth,
  735. u32 index)
  736. {
  737. char portIdStr[27];
  738. char *scopeStr;
  739. struct publication *publ = sseq->zone_list;
  740. tipc_printf(buf, "%-10u %-10u ", sseq->lower, sseq->upper);
  741. if (depth == 2 || !publ) {
  742. tipc_printf(buf, "\n");
  743. return;
  744. }
  745. do {
  746. sprintf (portIdStr, "<%u.%u.%u:%u>",
  747. tipc_zone(publ->node), tipc_cluster(publ->node),
  748. tipc_node(publ->node), publ->ref);
  749. tipc_printf(buf, "%-26s ", portIdStr);
  750. if (depth > 3) {
  751. if (publ->node != tipc_own_addr)
  752. scopeStr = "";
  753. else if (publ->scope == TIPC_NODE_SCOPE)
  754. scopeStr = "node";
  755. else if (publ->scope == TIPC_CLUSTER_SCOPE)
  756. scopeStr = "cluster";
  757. else
  758. scopeStr = "zone";
  759. tipc_printf(buf, "%-10u %s", publ->key, scopeStr);
  760. }
  761. publ = publ->zone_list_next;
  762. if (publ == sseq->zone_list)
  763. break;
  764. tipc_printf(buf, "\n%33s", " ");
  765. } while (1);
  766. tipc_printf(buf, "\n");
  767. }
  768. /**
  769. * nameseq_list: print specified name sequence contents into the given buffer
  770. */
  771. static void nameseq_list(struct name_seq *seq, struct print_buf *buf, u32 depth,
  772. u32 type, u32 lowbound, u32 upbound, u32 index)
  773. {
  774. struct sub_seq *sseq;
  775. char typearea[11];
  776. sprintf(typearea, "%-10u", seq->type);
  777. if (depth == 1) {
  778. tipc_printf(buf, "%s\n", typearea);
  779. return;
  780. }
  781. for (sseq = seq->sseqs; sseq != &seq->sseqs[seq->first_free]; sseq++) {
  782. if ((lowbound <= sseq->upper) && (upbound >= sseq->lower)) {
  783. tipc_printf(buf, "%s ", typearea);
  784. subseq_list(sseq, buf, depth, index);
  785. sprintf(typearea, "%10s", " ");
  786. }
  787. }
  788. }
  789. /**
  790. * nametbl_header - print name table header into the given buffer
  791. */
  792. static void nametbl_header(struct print_buf *buf, u32 depth)
  793. {
  794. tipc_printf(buf, "Type ");
  795. if (depth > 1)
  796. tipc_printf(buf, "Lower Upper ");
  797. if (depth > 2)
  798. tipc_printf(buf, "Port Identity ");
  799. if (depth > 3)
  800. tipc_printf(buf, "Publication");
  801. tipc_printf(buf, "\n-----------");
  802. if (depth > 1)
  803. tipc_printf(buf, "--------------------- ");
  804. if (depth > 2)
  805. tipc_printf(buf, "-------------------------- ");
  806. if (depth > 3)
  807. tipc_printf(buf, "------------------");
  808. tipc_printf(buf, "\n");
  809. }
  810. /**
  811. * nametbl_list - print specified name table contents into the given buffer
  812. */
  813. static void nametbl_list(struct print_buf *buf, u32 depth_info,
  814. u32 type, u32 lowbound, u32 upbound)
  815. {
  816. struct hlist_head *seq_head;
  817. struct hlist_node *seq_node;
  818. struct name_seq *seq;
  819. int all_types;
  820. u32 depth;
  821. u32 i;
  822. all_types = (depth_info & TIPC_NTQ_ALLTYPES);
  823. depth = (depth_info & ~TIPC_NTQ_ALLTYPES);
  824. if (depth == 0)
  825. return;
  826. if (all_types) {
  827. /* display all entries in name table to specified depth */
  828. nametbl_header(buf, depth);
  829. lowbound = 0;
  830. upbound = ~0;
  831. for (i = 0; i < tipc_nametbl_size; i++) {
  832. seq_head = &table.types[i];
  833. hlist_for_each_entry(seq, seq_node, seq_head, ns_list) {
  834. nameseq_list(seq, buf, depth, seq->type,
  835. lowbound, upbound, i);
  836. }
  837. }
  838. } else {
  839. /* display only the sequence that matches the specified type */
  840. if (upbound < lowbound) {
  841. tipc_printf(buf, "invalid name sequence specified\n");
  842. return;
  843. }
  844. nametbl_header(buf, depth);
  845. i = hash(type);
  846. seq_head = &table.types[i];
  847. hlist_for_each_entry(seq, seq_node, seq_head, ns_list) {
  848. if (seq->type == type) {
  849. nameseq_list(seq, buf, depth, type,
  850. lowbound, upbound, i);
  851. break;
  852. }
  853. }
  854. }
  855. }
  856. #if 0
  857. void tipc_nametbl_print(struct print_buf *buf, const char *str)
  858. {
  859. tipc_printf(buf, str);
  860. read_lock_bh(&tipc_nametbl_lock);
  861. nametbl_list(buf, 0, 0, 0, 0);
  862. read_unlock_bh(&tipc_nametbl_lock);
  863. }
  864. #endif
  865. #define MAX_NAME_TBL_QUERY 32768
  866. struct sk_buff *tipc_nametbl_get(const void *req_tlv_area, int req_tlv_space)
  867. {
  868. struct sk_buff *buf;
  869. struct tipc_name_table_query *argv;
  870. struct tlv_desc *rep_tlv;
  871. struct print_buf b;
  872. int str_len;
  873. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NAME_TBL_QUERY))
  874. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  875. buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_NAME_TBL_QUERY));
  876. if (!buf)
  877. return NULL;
  878. rep_tlv = (struct tlv_desc *)buf->data;
  879. tipc_printbuf_init(&b, TLV_DATA(rep_tlv), MAX_NAME_TBL_QUERY);
  880. argv = (struct tipc_name_table_query *)TLV_DATA(req_tlv_area);
  881. read_lock_bh(&tipc_nametbl_lock);
  882. nametbl_list(&b, ntohl(argv->depth), ntohl(argv->type),
  883. ntohl(argv->lowbound), ntohl(argv->upbound));
  884. read_unlock_bh(&tipc_nametbl_lock);
  885. str_len = tipc_printbuf_validate(&b);
  886. skb_put(buf, TLV_SPACE(str_len));
  887. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  888. return buf;
  889. }
  890. #if 0
  891. void tipc_nametbl_dump(void)
  892. {
  893. nametbl_list(TIPC_CONS, 0, 0, 0, 0);
  894. }
  895. #endif
  896. int tipc_nametbl_init(void)
  897. {
  898. int array_size = sizeof(struct hlist_head) * tipc_nametbl_size;
  899. table.types = (struct hlist_head *)kmalloc(array_size, GFP_ATOMIC);
  900. if (!table.types)
  901. return -ENOMEM;
  902. write_lock_bh(&tipc_nametbl_lock);
  903. memset(table.types, 0, array_size);
  904. table.local_publ_count = 0;
  905. write_unlock_bh(&tipc_nametbl_lock);
  906. return 0;
  907. }
  908. void tipc_nametbl_stop(void)
  909. {
  910. struct hlist_head *seq_head;
  911. struct hlist_node *seq_node;
  912. struct hlist_node *tmp;
  913. struct name_seq *seq;
  914. u32 i;
  915. if (!table.types)
  916. return;
  917. write_lock_bh(&tipc_nametbl_lock);
  918. for (i = 0; i < tipc_nametbl_size; i++) {
  919. seq_head = &table.types[i];
  920. hlist_for_each_entry_safe(seq, seq_node, tmp, seq_head, ns_list) {
  921. struct sub_seq *sseq = seq->sseqs;
  922. for (; sseq != &seq->sseqs[seq->first_free]; sseq++) {
  923. struct publication *publ = sseq->zone_list;
  924. assert(publ);
  925. do {
  926. struct publication *next =
  927. publ->zone_list_next;
  928. kfree(publ);
  929. publ = next;
  930. }
  931. while (publ != sseq->zone_list);
  932. }
  933. }
  934. }
  935. kfree(table.types);
  936. table.types = NULL;
  937. write_unlock_bh(&tipc_nametbl_lock);
  938. }