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