name_table.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. /*
  2. * net/tipc/name_table.c: TIPC name table code
  3. *
  4. * Copyright (c) 2000-2006, Ericsson AB
  5. * Copyright (c) 2004-2008, 2010-2011, Wind River Systems
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the names of the copyright holders nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * Alternatively, this software may be distributed under the terms of the
  21. * GNU General Public License ("GPL") version 2 as published by the Free
  22. * Software Foundation.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include "core.h"
  37. #include "config.h"
  38. #include "name_table.h"
  39. #include "name_distr.h"
  40. #include "subscr.h"
  41. #include "port.h"
  42. static int tipc_nametbl_size = 1024; /* must be a power of 2 */
  43. /**
  44. * struct name_info - name sequence publication info
  45. * @node_list: circular list of publications made by own node
  46. * @cluster_list: circular list of publications made by own cluster
  47. * @zone_list: circular list of publications made by own zone
  48. * @node_list_size: number of entries in "node_list"
  49. * @cluster_list_size: number of entries in "cluster_list"
  50. * @zone_list_size: number of entries in "zone_list"
  51. *
  52. * Note: The zone list always contains at least one entry, since all
  53. * publications of the associated name sequence belong to it.
  54. * (The cluster and node lists may be empty.)
  55. */
  56. struct name_info {
  57. struct list_head node_list;
  58. struct list_head cluster_list;
  59. struct list_head zone_list;
  60. u32 node_list_size;
  61. u32 cluster_list_size;
  62. u32 zone_list_size;
  63. };
  64. /**
  65. * struct sub_seq - container for all published instances of a name sequence
  66. * @lower: name sequence lower bound
  67. * @upper: name sequence upper bound
  68. * @info: pointer to name sequence publication info
  69. */
  70. struct sub_seq {
  71. u32 lower;
  72. u32 upper;
  73. struct name_info *info;
  74. };
  75. /**
  76. * struct name_seq - container for all published instances of a name type
  77. * @type: 32 bit 'type' value for name sequence
  78. * @sseq: pointer to dynamically-sized array of sub-sequences of this 'type';
  79. * sub-sequences are sorted in ascending order
  80. * @alloc: number of sub-sequences currently in array
  81. * @first_free: array index of first unused sub-sequence entry
  82. * @ns_list: links to adjacent name sequences in hash chain
  83. * @subscriptions: list of subscriptions for this 'type'
  84. * @lock: spinlock controlling access to publication lists of all sub-sequences
  85. */
  86. struct name_seq {
  87. u32 type;
  88. struct sub_seq *sseqs;
  89. u32 alloc;
  90. u32 first_free;
  91. struct hlist_node ns_list;
  92. struct list_head subscriptions;
  93. spinlock_t lock;
  94. };
  95. /**
  96. * struct name_table - table containing all existing port name publications
  97. * @types: pointer to fixed-sized array of name sequence lists,
  98. * accessed via hashing on 'type'; name sequence lists are *not* sorted
  99. * @local_publ_count: number of publications issued by this node
  100. */
  101. struct name_table {
  102. struct hlist_head *types;
  103. u32 local_publ_count;
  104. };
  105. static struct name_table table;
  106. static atomic_t rsv_publ_ok = ATOMIC_INIT(0);
  107. DEFINE_RWLOCK(tipc_nametbl_lock);
  108. static int hash(int x)
  109. {
  110. return x & (tipc_nametbl_size - 1);
  111. }
  112. /**
  113. * publ_create - create a publication structure
  114. */
  115. static struct publication *publ_create(u32 type, u32 lower, u32 upper,
  116. u32 scope, u32 node, u32 port_ref,
  117. u32 key)
  118. {
  119. struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
  120. if (publ == NULL) {
  121. warn("Publication creation failure, no memory\n");
  122. return NULL;
  123. }
  124. publ->type = type;
  125. publ->lower = lower;
  126. publ->upper = upper;
  127. publ->scope = scope;
  128. publ->node = node;
  129. publ->ref = port_ref;
  130. publ->key = key;
  131. INIT_LIST_HEAD(&publ->local_list);
  132. INIT_LIST_HEAD(&publ->pport_list);
  133. INIT_LIST_HEAD(&publ->subscr.nodesub_list);
  134. return publ;
  135. }
  136. /**
  137. * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
  138. */
  139. static struct sub_seq *tipc_subseq_alloc(u32 cnt)
  140. {
  141. struct sub_seq *sseq = kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC);
  142. return sseq;
  143. }
  144. /**
  145. * tipc_nameseq_create - create a name sequence structure for the specified 'type'
  146. *
  147. * Allocates a single sub-sequence structure and sets it to all 0's.
  148. */
  149. static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head)
  150. {
  151. struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC);
  152. struct sub_seq *sseq = tipc_subseq_alloc(1);
  153. if (!nseq || !sseq) {
  154. warn("Name sequence creation failed, no memory\n");
  155. kfree(nseq);
  156. kfree(sseq);
  157. return NULL;
  158. }
  159. spin_lock_init(&nseq->lock);
  160. nseq->type = type;
  161. nseq->sseqs = sseq;
  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. struct name_info *info;
  229. int created_subseq = 0;
  230. sseq = nameseq_find_subseq(nseq, lower);
  231. if (sseq) {
  232. /* Lower end overlaps existing entry => need an exact match */
  233. if ((sseq->lower != lower) || (sseq->upper != upper)) {
  234. warn("Cannot publish {%u,%u,%u}, overlap error\n",
  235. type, lower, upper);
  236. return NULL;
  237. }
  238. info = sseq->info;
  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. memcpy(sseqs, nseq->sseqs,
  260. nseq->alloc * sizeof(struct sub_seq));
  261. kfree(nseq->sseqs);
  262. nseq->sseqs = sseqs;
  263. nseq->alloc *= 2;
  264. }
  265. info = kzalloc(sizeof(*info), GFP_ATOMIC);
  266. if (!info) {
  267. warn("Cannot publish {%u,%u,%u}, no memory\n",
  268. type, lower, upper);
  269. return NULL;
  270. }
  271. INIT_LIST_HEAD(&info->node_list);
  272. INIT_LIST_HEAD(&info->cluster_list);
  273. INIT_LIST_HEAD(&info->zone_list);
  274. /* Insert new sub-sequence */
  275. sseq = &nseq->sseqs[inspos];
  276. freesseq = &nseq->sseqs[nseq->first_free];
  277. memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof(*sseq));
  278. memset(sseq, 0, sizeof(*sseq));
  279. nseq->first_free++;
  280. sseq->lower = lower;
  281. sseq->upper = upper;
  282. sseq->info = info;
  283. created_subseq = 1;
  284. }
  285. /* Insert a publication: */
  286. publ = publ_create(type, lower, upper, scope, node, port, key);
  287. if (!publ)
  288. return NULL;
  289. list_add(&publ->zone_list, &info->zone_list);
  290. info->zone_list_size++;
  291. if (in_own_cluster(node)) {
  292. list_add(&publ->cluster_list, &info->cluster_list);
  293. info->cluster_list_size++;
  294. }
  295. if (node == tipc_own_addr) {
  296. list_add(&publ->node_list, &info->node_list);
  297. info->node_list_size++;
  298. }
  299. /*
  300. * Any subscriptions waiting for notification?
  301. */
  302. list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
  303. tipc_subscr_report_overlap(s,
  304. publ->lower,
  305. publ->upper,
  306. TIPC_PUBLISHED,
  307. publ->ref,
  308. publ->node,
  309. created_subseq);
  310. }
  311. return publ;
  312. }
  313. /**
  314. * tipc_nameseq_remove_publ -
  315. *
  316. * NOTE: There may be cases where TIPC is asked to remove a publication
  317. * that is not in the name table. For example, if another node issues a
  318. * publication for a name sequence that overlaps an existing name sequence
  319. * the publication will not be recorded, which means the publication won't
  320. * be found when the name sequence is later withdrawn by that node.
  321. * A failed withdraw request simply returns a failure indication and lets the
  322. * caller issue any error or warning messages associated with such a problem.
  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 sub_seq *sseq = nameseq_find_subseq(nseq, inst);
  329. struct name_info *info;
  330. struct sub_seq *free;
  331. struct subscription *s, *st;
  332. int removed_subseq = 0;
  333. if (!sseq)
  334. return NULL;
  335. info = sseq->info;
  336. /* Locate publication, if it exists */
  337. list_for_each_entry(publ, &info->zone_list, zone_list) {
  338. if ((publ->key == key) && (publ->ref == ref) &&
  339. (!publ->node || (publ->node == node)))
  340. goto found;
  341. }
  342. return NULL;
  343. found:
  344. /* Remove publication from zone scope list */
  345. list_del(&publ->zone_list);
  346. info->zone_list_size--;
  347. /* Remove publication from cluster scope list, if present */
  348. if (in_own_cluster(node)) {
  349. list_del(&publ->cluster_list);
  350. info->cluster_list_size--;
  351. }
  352. /* Remove publication from node scope list, if present */
  353. if (node == tipc_own_addr) {
  354. list_del(&publ->node_list);
  355. info->node_list_size--;
  356. }
  357. /* Contract subseq list if no more publications for that subseq */
  358. if (list_empty(&info->zone_list)) {
  359. kfree(info);
  360. free = &nseq->sseqs[nseq->first_free--];
  361. memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq));
  362. removed_subseq = 1;
  363. }
  364. /* Notify any waiting subscriptions */
  365. list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
  366. tipc_subscr_report_overlap(s,
  367. publ->lower,
  368. publ->upper,
  369. TIPC_WITHDRAWN,
  370. publ->ref,
  371. publ->node,
  372. removed_subseq);
  373. }
  374. return publ;
  375. }
  376. /**
  377. * tipc_nameseq_subscribe: attach a subscription, and issue
  378. * the prescribed number of events if there is any sub-
  379. * sequence overlapping with the requested sequence
  380. */
  381. static void tipc_nameseq_subscribe(struct name_seq *nseq, struct subscription *s)
  382. {
  383. struct sub_seq *sseq = nseq->sseqs;
  384. list_add(&s->nameseq_list, &nseq->subscriptions);
  385. if (!sseq)
  386. return;
  387. while (sseq != &nseq->sseqs[nseq->first_free]) {
  388. if (tipc_subscr_overlap(s, sseq->lower, sseq->upper)) {
  389. struct publication *crs;
  390. struct name_info *info = sseq->info;
  391. int must_report = 1;
  392. list_for_each_entry(crs, &info->zone_list, zone_list) {
  393. tipc_subscr_report_overlap(s,
  394. sseq->lower,
  395. sseq->upper,
  396. TIPC_PUBLISHED,
  397. crs->ref,
  398. crs->node,
  399. must_report);
  400. must_report = 0;
  401. }
  402. }
  403. sseq++;
  404. }
  405. }
  406. static struct name_seq *nametbl_find_seq(u32 type)
  407. {
  408. struct hlist_head *seq_head;
  409. struct hlist_node *seq_node;
  410. struct name_seq *ns;
  411. seq_head = &table.types[hash(type)];
  412. hlist_for_each_entry(ns, seq_node, seq_head, ns_list) {
  413. if (ns->type == type)
  414. return ns;
  415. }
  416. return NULL;
  417. };
  418. struct publication *tipc_nametbl_insert_publ(u32 type, u32 lower, u32 upper,
  419. u32 scope, u32 node, u32 port, u32 key)
  420. {
  421. struct name_seq *seq = nametbl_find_seq(type);
  422. if (lower > upper) {
  423. warn("Failed to publish illegal {%u,%u,%u}\n",
  424. type, lower, upper);
  425. return NULL;
  426. }
  427. if (!seq)
  428. seq = tipc_nameseq_create(type, &table.types[hash(type)]);
  429. if (!seq)
  430. return NULL;
  431. return tipc_nameseq_insert_publ(seq, type, lower, upper,
  432. scope, node, port, key);
  433. }
  434. struct publication *tipc_nametbl_remove_publ(u32 type, u32 lower,
  435. u32 node, u32 ref, u32 key)
  436. {
  437. struct publication *publ;
  438. struct name_seq *seq = nametbl_find_seq(type);
  439. if (!seq)
  440. return NULL;
  441. publ = tipc_nameseq_remove_publ(seq, lower, node, ref, key);
  442. if (!seq->first_free && list_empty(&seq->subscriptions)) {
  443. hlist_del_init(&seq->ns_list);
  444. kfree(seq->sseqs);
  445. kfree(seq);
  446. }
  447. return publ;
  448. }
  449. /*
  450. * tipc_nametbl_translate - translate name to port id
  451. *
  452. * Note: on entry 'destnode' is the search domain used during translation;
  453. * on exit it passes back the node address of the matching port (if any)
  454. */
  455. u32 tipc_nametbl_translate(u32 type, u32 instance, u32 *destnode)
  456. {
  457. struct sub_seq *sseq;
  458. struct name_info *info;
  459. struct publication *publ;
  460. struct name_seq *seq;
  461. u32 ref = 0;
  462. if (!tipc_in_scope(*destnode, tipc_own_addr))
  463. return 0;
  464. read_lock_bh(&tipc_nametbl_lock);
  465. seq = nametbl_find_seq(type);
  466. if (unlikely(!seq))
  467. goto not_found;
  468. sseq = nameseq_find_subseq(seq, instance);
  469. if (unlikely(!sseq))
  470. goto not_found;
  471. spin_lock_bh(&seq->lock);
  472. info = sseq->info;
  473. /* Closest-First Algorithm: */
  474. if (likely(!*destnode)) {
  475. if (!list_empty(&info->node_list)) {
  476. publ = list_first_entry(&info->node_list,
  477. struct publication,
  478. node_list);
  479. list_move_tail(&publ->node_list,
  480. &info->node_list);
  481. } else if (!list_empty(&info->cluster_list)) {
  482. publ = list_first_entry(&info->cluster_list,
  483. struct publication,
  484. cluster_list);
  485. list_move_tail(&publ->cluster_list,
  486. &info->cluster_list);
  487. } else {
  488. publ = list_first_entry(&info->zone_list,
  489. struct publication,
  490. zone_list);
  491. list_move_tail(&publ->zone_list,
  492. &info->zone_list);
  493. }
  494. }
  495. /* Round-Robin Algorithm: */
  496. else if (*destnode == tipc_own_addr) {
  497. if (list_empty(&info->node_list))
  498. goto no_match;
  499. publ = list_first_entry(&info->node_list, struct publication,
  500. node_list);
  501. list_move_tail(&publ->node_list, &info->node_list);
  502. } else if (in_own_cluster(*destnode)) {
  503. if (list_empty(&info->cluster_list))
  504. goto no_match;
  505. publ = list_first_entry(&info->cluster_list, struct publication,
  506. cluster_list);
  507. list_move_tail(&publ->cluster_list, &info->cluster_list);
  508. } else {
  509. publ = list_first_entry(&info->zone_list, struct publication,
  510. zone_list);
  511. list_move_tail(&publ->zone_list, &info->zone_list);
  512. }
  513. ref = publ->ref;
  514. *destnode = publ->node;
  515. no_match:
  516. spin_unlock_bh(&seq->lock);
  517. not_found:
  518. read_unlock_bh(&tipc_nametbl_lock);
  519. return ref;
  520. }
  521. /**
  522. * tipc_nametbl_mc_translate - find multicast destinations
  523. *
  524. * Creates list of all local ports that overlap the given multicast address;
  525. * also determines if any off-node ports overlap.
  526. *
  527. * Note: Publications with a scope narrower than 'limit' are ignored.
  528. * (i.e. local node-scope publications mustn't receive messages arriving
  529. * from another node, even if the multcast link brought it here)
  530. *
  531. * Returns non-zero if any off-node ports overlap
  532. */
  533. int tipc_nametbl_mc_translate(u32 type, u32 lower, u32 upper, u32 limit,
  534. struct port_list *dports)
  535. {
  536. struct name_seq *seq;
  537. struct sub_seq *sseq;
  538. struct sub_seq *sseq_stop;
  539. struct name_info *info;
  540. int res = 0;
  541. read_lock_bh(&tipc_nametbl_lock);
  542. seq = nametbl_find_seq(type);
  543. if (!seq)
  544. goto exit;
  545. spin_lock_bh(&seq->lock);
  546. sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
  547. sseq_stop = seq->sseqs + seq->first_free;
  548. for (; sseq != sseq_stop; sseq++) {
  549. struct publication *publ;
  550. if (sseq->lower > upper)
  551. break;
  552. info = sseq->info;
  553. list_for_each_entry(publ, &info->node_list, node_list) {
  554. if (publ->scope <= limit)
  555. tipc_port_list_add(dports, publ->ref);
  556. }
  557. if (info->cluster_list_size != info->node_list_size)
  558. res = 1;
  559. }
  560. spin_unlock_bh(&seq->lock);
  561. exit:
  562. read_unlock_bh(&tipc_nametbl_lock);
  563. return res;
  564. }
  565. /**
  566. * tipc_nametbl_publish_rsv - publish port name using a reserved name type
  567. */
  568. int tipc_nametbl_publish_rsv(u32 ref, unsigned int scope,
  569. struct tipc_name_seq const *seq)
  570. {
  571. int res;
  572. atomic_inc(&rsv_publ_ok);
  573. res = tipc_publish(ref, scope, seq);
  574. atomic_dec(&rsv_publ_ok);
  575. return res;
  576. }
  577. /**
  578. * tipc_nametbl_publish - add name publication to network name tables
  579. */
  580. struct publication *tipc_nametbl_publish(u32 type, u32 lower, u32 upper,
  581. u32 scope, u32 port_ref, u32 key)
  582. {
  583. struct publication *publ;
  584. if (table.local_publ_count >= tipc_max_publications) {
  585. warn("Publication failed, local publication limit reached (%u)\n",
  586. tipc_max_publications);
  587. return NULL;
  588. }
  589. if ((type < TIPC_RESERVED_TYPES) && !atomic_read(&rsv_publ_ok)) {
  590. warn("Publication failed, reserved name {%u,%u,%u}\n",
  591. type, lower, upper);
  592. return NULL;
  593. }
  594. write_lock_bh(&tipc_nametbl_lock);
  595. table.local_publ_count++;
  596. publ = tipc_nametbl_insert_publ(type, lower, upper, scope,
  597. tipc_own_addr, port_ref, key);
  598. if (publ && (scope != TIPC_NODE_SCOPE))
  599. tipc_named_publish(publ);
  600. write_unlock_bh(&tipc_nametbl_lock);
  601. return publ;
  602. }
  603. /**
  604. * tipc_nametbl_withdraw - withdraw name publication from network name tables
  605. */
  606. int tipc_nametbl_withdraw(u32 type, u32 lower, u32 ref, u32 key)
  607. {
  608. struct publication *publ;
  609. write_lock_bh(&tipc_nametbl_lock);
  610. publ = tipc_nametbl_remove_publ(type, lower, tipc_own_addr, ref, key);
  611. if (likely(publ)) {
  612. table.local_publ_count--;
  613. if (publ->scope != TIPC_NODE_SCOPE)
  614. tipc_named_withdraw(publ);
  615. write_unlock_bh(&tipc_nametbl_lock);
  616. list_del_init(&publ->pport_list);
  617. kfree(publ);
  618. return 1;
  619. }
  620. write_unlock_bh(&tipc_nametbl_lock);
  621. err("Unable to remove local publication\n"
  622. "(type=%u, lower=%u, ref=%u, key=%u)\n",
  623. type, lower, ref, key);
  624. return 0;
  625. }
  626. /**
  627. * tipc_nametbl_subscribe - add a subscription object to the name table
  628. */
  629. void tipc_nametbl_subscribe(struct subscription *s)
  630. {
  631. u32 type = s->seq.type;
  632. struct name_seq *seq;
  633. write_lock_bh(&tipc_nametbl_lock);
  634. seq = nametbl_find_seq(type);
  635. if (!seq)
  636. seq = tipc_nameseq_create(type, &table.types[hash(type)]);
  637. if (seq) {
  638. spin_lock_bh(&seq->lock);
  639. tipc_nameseq_subscribe(seq, s);
  640. spin_unlock_bh(&seq->lock);
  641. } else {
  642. warn("Failed to create subscription for {%u,%u,%u}\n",
  643. s->seq.type, s->seq.lower, s->seq.upper);
  644. }
  645. write_unlock_bh(&tipc_nametbl_lock);
  646. }
  647. /**
  648. * tipc_nametbl_unsubscribe - remove a subscription object from name table
  649. */
  650. void tipc_nametbl_unsubscribe(struct subscription *s)
  651. {
  652. struct name_seq *seq;
  653. write_lock_bh(&tipc_nametbl_lock);
  654. seq = nametbl_find_seq(s->seq.type);
  655. if (seq != NULL) {
  656. spin_lock_bh(&seq->lock);
  657. list_del_init(&s->nameseq_list);
  658. spin_unlock_bh(&seq->lock);
  659. if ((seq->first_free == 0) && list_empty(&seq->subscriptions)) {
  660. hlist_del_init(&seq->ns_list);
  661. kfree(seq->sseqs);
  662. kfree(seq);
  663. }
  664. }
  665. write_unlock_bh(&tipc_nametbl_lock);
  666. }
  667. /**
  668. * subseq_list: print specified sub-sequence contents into the given buffer
  669. */
  670. static void subseq_list(struct sub_seq *sseq, struct print_buf *buf, u32 depth,
  671. u32 index)
  672. {
  673. char portIdStr[27];
  674. const char *scope_str[] = {"", " zone", " cluster", " node"};
  675. struct publication *publ;
  676. struct name_info *info;
  677. tipc_printf(buf, "%-10u %-10u ", sseq->lower, sseq->upper);
  678. if (depth == 2) {
  679. tipc_printf(buf, "\n");
  680. return;
  681. }
  682. info = sseq->info;
  683. list_for_each_entry(publ, &info->zone_list, zone_list) {
  684. sprintf(portIdStr, "<%u.%u.%u:%u>",
  685. tipc_zone(publ->node), tipc_cluster(publ->node),
  686. tipc_node(publ->node), publ->ref);
  687. tipc_printf(buf, "%-26s ", portIdStr);
  688. if (depth > 3) {
  689. tipc_printf(buf, "%-10u %s", publ->key,
  690. scope_str[publ->scope]);
  691. }
  692. if (!list_is_last(&publ->zone_list, &info->zone_list))
  693. tipc_printf(buf, "\n%33s", " ");
  694. };
  695. tipc_printf(buf, "\n");
  696. }
  697. /**
  698. * nameseq_list: print specified name sequence contents into the given buffer
  699. */
  700. static void nameseq_list(struct name_seq *seq, struct print_buf *buf, u32 depth,
  701. u32 type, u32 lowbound, u32 upbound, u32 index)
  702. {
  703. struct sub_seq *sseq;
  704. char typearea[11];
  705. if (seq->first_free == 0)
  706. return;
  707. sprintf(typearea, "%-10u", seq->type);
  708. if (depth == 1) {
  709. tipc_printf(buf, "%s\n", typearea);
  710. return;
  711. }
  712. for (sseq = seq->sseqs; sseq != &seq->sseqs[seq->first_free]; sseq++) {
  713. if ((lowbound <= sseq->upper) && (upbound >= sseq->lower)) {
  714. tipc_printf(buf, "%s ", typearea);
  715. spin_lock_bh(&seq->lock);
  716. subseq_list(sseq, buf, depth, index);
  717. spin_unlock_bh(&seq->lock);
  718. sprintf(typearea, "%10s", " ");
  719. }
  720. }
  721. }
  722. /**
  723. * nametbl_header - print name table header into the given buffer
  724. */
  725. static void nametbl_header(struct print_buf *buf, u32 depth)
  726. {
  727. const char *header[] = {
  728. "Type ",
  729. "Lower Upper ",
  730. "Port Identity ",
  731. "Publication Scope"
  732. };
  733. int i;
  734. if (depth > 4)
  735. depth = 4;
  736. for (i = 0; i < depth; i++)
  737. tipc_printf(buf, header[i]);
  738. tipc_printf(buf, "\n");
  739. }
  740. /**
  741. * nametbl_list - print specified name table contents into the given buffer
  742. */
  743. static void nametbl_list(struct print_buf *buf, u32 depth_info,
  744. u32 type, u32 lowbound, u32 upbound)
  745. {
  746. struct hlist_head *seq_head;
  747. struct hlist_node *seq_node;
  748. struct name_seq *seq;
  749. int all_types;
  750. u32 depth;
  751. u32 i;
  752. all_types = (depth_info & TIPC_NTQ_ALLTYPES);
  753. depth = (depth_info & ~TIPC_NTQ_ALLTYPES);
  754. if (depth == 0)
  755. return;
  756. if (all_types) {
  757. /* display all entries in name table to specified depth */
  758. nametbl_header(buf, depth);
  759. lowbound = 0;
  760. upbound = ~0;
  761. for (i = 0; i < tipc_nametbl_size; i++) {
  762. seq_head = &table.types[i];
  763. hlist_for_each_entry(seq, seq_node, seq_head, ns_list) {
  764. nameseq_list(seq, buf, depth, seq->type,
  765. lowbound, upbound, i);
  766. }
  767. }
  768. } else {
  769. /* display only the sequence that matches the specified type */
  770. if (upbound < lowbound) {
  771. tipc_printf(buf, "invalid name sequence specified\n");
  772. return;
  773. }
  774. nametbl_header(buf, depth);
  775. i = hash(type);
  776. seq_head = &table.types[i];
  777. hlist_for_each_entry(seq, seq_node, seq_head, ns_list) {
  778. if (seq->type == type) {
  779. nameseq_list(seq, buf, depth, type,
  780. lowbound, upbound, i);
  781. break;
  782. }
  783. }
  784. }
  785. }
  786. #define MAX_NAME_TBL_QUERY 32768
  787. struct sk_buff *tipc_nametbl_get(const void *req_tlv_area, int req_tlv_space)
  788. {
  789. struct sk_buff *buf;
  790. struct tipc_name_table_query *argv;
  791. struct tlv_desc *rep_tlv;
  792. struct print_buf b;
  793. int str_len;
  794. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NAME_TBL_QUERY))
  795. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  796. buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_NAME_TBL_QUERY));
  797. if (!buf)
  798. return NULL;
  799. rep_tlv = (struct tlv_desc *)buf->data;
  800. tipc_printbuf_init(&b, TLV_DATA(rep_tlv), MAX_NAME_TBL_QUERY);
  801. argv = (struct tipc_name_table_query *)TLV_DATA(req_tlv_area);
  802. read_lock_bh(&tipc_nametbl_lock);
  803. nametbl_list(&b, ntohl(argv->depth), ntohl(argv->type),
  804. ntohl(argv->lowbound), ntohl(argv->upbound));
  805. read_unlock_bh(&tipc_nametbl_lock);
  806. str_len = tipc_printbuf_validate(&b);
  807. skb_put(buf, TLV_SPACE(str_len));
  808. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  809. return buf;
  810. }
  811. int tipc_nametbl_init(void)
  812. {
  813. table.types = kcalloc(tipc_nametbl_size, sizeof(struct hlist_head),
  814. GFP_ATOMIC);
  815. if (!table.types)
  816. return -ENOMEM;
  817. table.local_publ_count = 0;
  818. return 0;
  819. }
  820. void tipc_nametbl_stop(void)
  821. {
  822. u32 i;
  823. if (!table.types)
  824. return;
  825. /* Verify name table is empty, then release it */
  826. write_lock_bh(&tipc_nametbl_lock);
  827. for (i = 0; i < tipc_nametbl_size; i++) {
  828. if (!hlist_empty(&table.types[i]))
  829. err("tipc_nametbl_stop(): hash chain %u is non-null\n", i);
  830. }
  831. kfree(table.types);
  832. table.types = NULL;
  833. write_unlock_bh(&tipc_nametbl_lock);
  834. }