name_table.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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. #define 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. DEFINE_RWLOCK(tipc_nametbl_lock);
  107. static int hash(int x)
  108. {
  109. return x & (TIPC_NAMETBL_SIZE - 1);
  110. }
  111. /**
  112. * publ_create - create a publication structure
  113. */
  114. static struct publication *publ_create(u32 type, u32 lower, u32 upper,
  115. u32 scope, u32 node, u32 port_ref,
  116. u32 key)
  117. {
  118. struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
  119. if (publ == NULL) {
  120. pr_warn("Publication creation failure, no memory\n");
  121. return NULL;
  122. }
  123. publ->type = type;
  124. publ->lower = lower;
  125. publ->upper = upper;
  126. publ->scope = scope;
  127. publ->node = node;
  128. publ->ref = port_ref;
  129. publ->key = key;
  130. INIT_LIST_HEAD(&publ->local_list);
  131. INIT_LIST_HEAD(&publ->pport_list);
  132. INIT_LIST_HEAD(&publ->subscr.nodesub_list);
  133. return publ;
  134. }
  135. /**
  136. * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
  137. */
  138. static struct sub_seq *tipc_subseq_alloc(u32 cnt)
  139. {
  140. struct sub_seq *sseq = kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC);
  141. return sseq;
  142. }
  143. /**
  144. * tipc_nameseq_create - create a name sequence structure for the specified 'type'
  145. *
  146. * Allocates a single sub-sequence structure and sets it to all 0's.
  147. */
  148. static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head)
  149. {
  150. struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC);
  151. struct sub_seq *sseq = tipc_subseq_alloc(1);
  152. if (!nseq || !sseq) {
  153. pr_warn("Name sequence creation failed, no memory\n");
  154. kfree(nseq);
  155. kfree(sseq);
  156. return NULL;
  157. }
  158. spin_lock_init(&nseq->lock);
  159. nseq->type = type;
  160. nseq->sseqs = sseq;
  161. nseq->alloc = 1;
  162. INIT_HLIST_NODE(&nseq->ns_list);
  163. INIT_LIST_HEAD(&nseq->subscriptions);
  164. hlist_add_head(&nseq->ns_list, seq_head);
  165. return nseq;
  166. }
  167. /*
  168. * nameseq_delete_empty - deletes a name sequence structure if now unused
  169. */
  170. static void nameseq_delete_empty(struct name_seq *seq)
  171. {
  172. if (!seq->first_free && list_empty(&seq->subscriptions)) {
  173. hlist_del_init(&seq->ns_list);
  174. kfree(seq->sseqs);
  175. kfree(seq);
  176. }
  177. }
  178. /**
  179. * nameseq_find_subseq - find sub-sequence (if any) matching a name instance
  180. *
  181. * Very time-critical, so binary searches through sub-sequence array.
  182. */
  183. static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq,
  184. u32 instance)
  185. {
  186. struct sub_seq *sseqs = nseq->sseqs;
  187. int low = 0;
  188. int high = nseq->first_free - 1;
  189. int mid;
  190. while (low <= high) {
  191. mid = (low + high) / 2;
  192. if (instance < sseqs[mid].lower)
  193. high = mid - 1;
  194. else if (instance > sseqs[mid].upper)
  195. low = mid + 1;
  196. else
  197. return &sseqs[mid];
  198. }
  199. return NULL;
  200. }
  201. /**
  202. * nameseq_locate_subseq - determine position of name instance in sub-sequence
  203. *
  204. * Returns index in sub-sequence array of the entry that contains the specified
  205. * instance value; if no entry contains that value, returns the position
  206. * where a new entry for it would be inserted in the array.
  207. *
  208. * Note: Similar to binary search code for locating a sub-sequence.
  209. */
  210. static u32 nameseq_locate_subseq(struct name_seq *nseq, u32 instance)
  211. {
  212. struct sub_seq *sseqs = nseq->sseqs;
  213. int low = 0;
  214. int high = nseq->first_free - 1;
  215. int mid;
  216. while (low <= high) {
  217. mid = (low + high) / 2;
  218. if (instance < sseqs[mid].lower)
  219. high = mid - 1;
  220. else if (instance > sseqs[mid].upper)
  221. low = mid + 1;
  222. else
  223. return mid;
  224. }
  225. return low;
  226. }
  227. /**
  228. * tipc_nameseq_insert_publ
  229. */
  230. static struct publication *tipc_nameseq_insert_publ(struct name_seq *nseq,
  231. u32 type, u32 lower, u32 upper,
  232. u32 scope, u32 node, u32 port, u32 key)
  233. {
  234. struct tipc_subscription *s;
  235. struct tipc_subscription *st;
  236. struct publication *publ;
  237. struct sub_seq *sseq;
  238. struct name_info *info;
  239. int created_subseq = 0;
  240. sseq = nameseq_find_subseq(nseq, lower);
  241. if (sseq) {
  242. /* Lower end overlaps existing entry => need an exact match */
  243. if ((sseq->lower != lower) || (sseq->upper != upper)) {
  244. pr_warn("Cannot publish {%u,%u,%u}, overlap error\n",
  245. type, lower, upper);
  246. return NULL;
  247. }
  248. info = sseq->info;
  249. /* Check if an identical publication already exists */
  250. list_for_each_entry(publ, &info->zone_list, zone_list) {
  251. if ((publ->ref == port) && (publ->key == key) &&
  252. (!publ->node || (publ->node == node)))
  253. return NULL;
  254. }
  255. } else {
  256. u32 inspos;
  257. struct sub_seq *freesseq;
  258. /* Find where lower end should be inserted */
  259. inspos = nameseq_locate_subseq(nseq, lower);
  260. /* Fail if upper end overlaps into an existing entry */
  261. if ((inspos < nseq->first_free) &&
  262. (upper >= nseq->sseqs[inspos].lower)) {
  263. pr_warn("Cannot publish {%u,%u,%u}, overlap error\n",
  264. type, lower, upper);
  265. return NULL;
  266. }
  267. /* Ensure there is space for new sub-sequence */
  268. if (nseq->first_free == nseq->alloc) {
  269. struct sub_seq *sseqs = tipc_subseq_alloc(nseq->alloc * 2);
  270. if (!sseqs) {
  271. pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
  272. type, lower, upper);
  273. return NULL;
  274. }
  275. memcpy(sseqs, nseq->sseqs,
  276. nseq->alloc * sizeof(struct sub_seq));
  277. kfree(nseq->sseqs);
  278. nseq->sseqs = sseqs;
  279. nseq->alloc *= 2;
  280. }
  281. info = kzalloc(sizeof(*info), GFP_ATOMIC);
  282. if (!info) {
  283. pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
  284. type, lower, upper);
  285. return NULL;
  286. }
  287. INIT_LIST_HEAD(&info->node_list);
  288. INIT_LIST_HEAD(&info->cluster_list);
  289. INIT_LIST_HEAD(&info->zone_list);
  290. /* Insert new sub-sequence */
  291. sseq = &nseq->sseqs[inspos];
  292. freesseq = &nseq->sseqs[nseq->first_free];
  293. memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof(*sseq));
  294. memset(sseq, 0, sizeof(*sseq));
  295. nseq->first_free++;
  296. sseq->lower = lower;
  297. sseq->upper = upper;
  298. sseq->info = info;
  299. created_subseq = 1;
  300. }
  301. /* Insert a publication */
  302. publ = publ_create(type, lower, upper, scope, node, port, key);
  303. if (!publ)
  304. return NULL;
  305. list_add(&publ->zone_list, &info->zone_list);
  306. info->zone_list_size++;
  307. if (in_own_cluster(node)) {
  308. list_add(&publ->cluster_list, &info->cluster_list);
  309. info->cluster_list_size++;
  310. }
  311. if (in_own_node(node)) {
  312. list_add(&publ->node_list, &info->node_list);
  313. info->node_list_size++;
  314. }
  315. /* Any subscriptions waiting for notification? */
  316. list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
  317. tipc_subscr_report_overlap(s,
  318. publ->lower,
  319. publ->upper,
  320. TIPC_PUBLISHED,
  321. publ->ref,
  322. publ->node,
  323. created_subseq);
  324. }
  325. return publ;
  326. }
  327. /**
  328. * tipc_nameseq_remove_publ
  329. *
  330. * NOTE: There may be cases where TIPC is asked to remove a publication
  331. * that is not in the name table. For example, if another node issues a
  332. * publication for a name sequence that overlaps an existing name sequence
  333. * the publication will not be recorded, which means the publication won't
  334. * be found when the name sequence is later withdrawn by that node.
  335. * A failed withdraw request simply returns a failure indication and lets the
  336. * caller issue any error or warning messages associated with such a problem.
  337. */
  338. static struct publication *tipc_nameseq_remove_publ(struct name_seq *nseq, u32 inst,
  339. u32 node, u32 ref, u32 key)
  340. {
  341. struct publication *publ;
  342. struct sub_seq *sseq = nameseq_find_subseq(nseq, inst);
  343. struct name_info *info;
  344. struct sub_seq *free;
  345. struct tipc_subscription *s, *st;
  346. int removed_subseq = 0;
  347. if (!sseq)
  348. return NULL;
  349. info = sseq->info;
  350. /* Locate publication, if it exists */
  351. list_for_each_entry(publ, &info->zone_list, zone_list) {
  352. if ((publ->key == key) && (publ->ref == ref) &&
  353. (!publ->node || (publ->node == node)))
  354. goto found;
  355. }
  356. return NULL;
  357. found:
  358. /* Remove publication from zone scope list */
  359. list_del(&publ->zone_list);
  360. info->zone_list_size--;
  361. /* Remove publication from cluster scope list, if present */
  362. if (in_own_cluster(node)) {
  363. list_del(&publ->cluster_list);
  364. info->cluster_list_size--;
  365. }
  366. /* Remove publication from node scope list, if present */
  367. if (in_own_node(node)) {
  368. list_del(&publ->node_list);
  369. info->node_list_size--;
  370. }
  371. /* Contract subseq list if no more publications for that subseq */
  372. if (list_empty(&info->zone_list)) {
  373. kfree(info);
  374. free = &nseq->sseqs[nseq->first_free--];
  375. memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq));
  376. removed_subseq = 1;
  377. }
  378. /* Notify any waiting subscriptions */
  379. list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
  380. tipc_subscr_report_overlap(s,
  381. publ->lower,
  382. publ->upper,
  383. TIPC_WITHDRAWN,
  384. publ->ref,
  385. publ->node,
  386. removed_subseq);
  387. }
  388. return publ;
  389. }
  390. /**
  391. * tipc_nameseq_subscribe - attach a subscription, and issue
  392. * the prescribed number of events if there is any sub-
  393. * sequence overlapping with the requested sequence
  394. */
  395. static void tipc_nameseq_subscribe(struct name_seq *nseq,
  396. struct tipc_subscription *s)
  397. {
  398. struct sub_seq *sseq = nseq->sseqs;
  399. list_add(&s->nameseq_list, &nseq->subscriptions);
  400. if (!sseq)
  401. return;
  402. while (sseq != &nseq->sseqs[nseq->first_free]) {
  403. if (tipc_subscr_overlap(s, sseq->lower, sseq->upper)) {
  404. struct publication *crs;
  405. struct name_info *info = sseq->info;
  406. int must_report = 1;
  407. list_for_each_entry(crs, &info->zone_list, zone_list) {
  408. tipc_subscr_report_overlap(s,
  409. sseq->lower,
  410. sseq->upper,
  411. TIPC_PUBLISHED,
  412. crs->ref,
  413. crs->node,
  414. must_report);
  415. must_report = 0;
  416. }
  417. }
  418. sseq++;
  419. }
  420. }
  421. static struct name_seq *nametbl_find_seq(u32 type)
  422. {
  423. struct hlist_head *seq_head;
  424. struct name_seq *ns;
  425. seq_head = &table.types[hash(type)];
  426. hlist_for_each_entry(ns, seq_head, ns_list) {
  427. if (ns->type == type)
  428. return ns;
  429. }
  430. return NULL;
  431. };
  432. struct publication *tipc_nametbl_insert_publ(u32 type, u32 lower, u32 upper,
  433. u32 scope, u32 node, u32 port, u32 key)
  434. {
  435. struct name_seq *seq = nametbl_find_seq(type);
  436. if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE) ||
  437. (lower > upper)) {
  438. pr_debug("Failed to publish illegal {%u,%u,%u} with scope %u\n",
  439. type, lower, upper, scope);
  440. return NULL;
  441. }
  442. if (!seq)
  443. seq = tipc_nameseq_create(type, &table.types[hash(type)]);
  444. if (!seq)
  445. return NULL;
  446. return tipc_nameseq_insert_publ(seq, type, lower, upper,
  447. scope, node, port, key);
  448. }
  449. struct publication *tipc_nametbl_remove_publ(u32 type, u32 lower,
  450. u32 node, u32 ref, u32 key)
  451. {
  452. struct publication *publ;
  453. struct name_seq *seq = nametbl_find_seq(type);
  454. if (!seq)
  455. return NULL;
  456. publ = tipc_nameseq_remove_publ(seq, lower, node, ref, key);
  457. nameseq_delete_empty(seq);
  458. return publ;
  459. }
  460. /**
  461. * tipc_nametbl_translate - perform name translation
  462. *
  463. * On entry, 'destnode' is the search domain used during translation.
  464. *
  465. * On exit:
  466. * - if name translation is deferred to another node/cluster/zone,
  467. * leaves 'destnode' unchanged (will be non-zero) and returns 0
  468. * - if name translation is attempted and succeeds, sets 'destnode'
  469. * to publishing node and returns port reference (will be non-zero)
  470. * - if name translation is attempted and fails, sets 'destnode' to 0
  471. * and returns 0
  472. */
  473. u32 tipc_nametbl_translate(u32 type, u32 instance, u32 *destnode)
  474. {
  475. struct sub_seq *sseq;
  476. struct name_info *info;
  477. struct publication *publ;
  478. struct name_seq *seq;
  479. u32 ref = 0;
  480. u32 node = 0;
  481. if (!tipc_in_scope(*destnode, tipc_own_addr))
  482. return 0;
  483. read_lock_bh(&tipc_nametbl_lock);
  484. seq = nametbl_find_seq(type);
  485. if (unlikely(!seq))
  486. goto not_found;
  487. sseq = nameseq_find_subseq(seq, instance);
  488. if (unlikely(!sseq))
  489. goto not_found;
  490. spin_lock_bh(&seq->lock);
  491. info = sseq->info;
  492. /* Closest-First Algorithm */
  493. if (likely(!*destnode)) {
  494. if (!list_empty(&info->node_list)) {
  495. publ = list_first_entry(&info->node_list,
  496. struct publication,
  497. node_list);
  498. list_move_tail(&publ->node_list,
  499. &info->node_list);
  500. } else if (!list_empty(&info->cluster_list)) {
  501. publ = list_first_entry(&info->cluster_list,
  502. struct publication,
  503. cluster_list);
  504. list_move_tail(&publ->cluster_list,
  505. &info->cluster_list);
  506. } else {
  507. publ = list_first_entry(&info->zone_list,
  508. struct publication,
  509. zone_list);
  510. list_move_tail(&publ->zone_list,
  511. &info->zone_list);
  512. }
  513. }
  514. /* Round-Robin Algorithm */
  515. else if (*destnode == tipc_own_addr) {
  516. if (list_empty(&info->node_list))
  517. goto no_match;
  518. publ = list_first_entry(&info->node_list, struct publication,
  519. node_list);
  520. list_move_tail(&publ->node_list, &info->node_list);
  521. } else if (in_own_cluster_exact(*destnode)) {
  522. if (list_empty(&info->cluster_list))
  523. goto no_match;
  524. publ = list_first_entry(&info->cluster_list, struct publication,
  525. cluster_list);
  526. list_move_tail(&publ->cluster_list, &info->cluster_list);
  527. } else {
  528. publ = list_first_entry(&info->zone_list, struct publication,
  529. zone_list);
  530. list_move_tail(&publ->zone_list, &info->zone_list);
  531. }
  532. ref = publ->ref;
  533. node = publ->node;
  534. no_match:
  535. spin_unlock_bh(&seq->lock);
  536. not_found:
  537. read_unlock_bh(&tipc_nametbl_lock);
  538. *destnode = node;
  539. return ref;
  540. }
  541. /**
  542. * tipc_nametbl_mc_translate - find multicast destinations
  543. *
  544. * Creates list of all local ports that overlap the given multicast address;
  545. * also determines if any off-node ports overlap.
  546. *
  547. * Note: Publications with a scope narrower than 'limit' are ignored.
  548. * (i.e. local node-scope publications mustn't receive messages arriving
  549. * from another node, even if the multcast link brought it here)
  550. *
  551. * Returns non-zero if any off-node ports overlap
  552. */
  553. int tipc_nametbl_mc_translate(u32 type, u32 lower, u32 upper, u32 limit,
  554. struct tipc_port_list *dports)
  555. {
  556. struct name_seq *seq;
  557. struct sub_seq *sseq;
  558. struct sub_seq *sseq_stop;
  559. struct name_info *info;
  560. int res = 0;
  561. read_lock_bh(&tipc_nametbl_lock);
  562. seq = nametbl_find_seq(type);
  563. if (!seq)
  564. goto exit;
  565. spin_lock_bh(&seq->lock);
  566. sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
  567. sseq_stop = seq->sseqs + seq->first_free;
  568. for (; sseq != sseq_stop; sseq++) {
  569. struct publication *publ;
  570. if (sseq->lower > upper)
  571. break;
  572. info = sseq->info;
  573. list_for_each_entry(publ, &info->node_list, node_list) {
  574. if (publ->scope <= limit)
  575. tipc_port_list_add(dports, publ->ref);
  576. }
  577. if (info->cluster_list_size != info->node_list_size)
  578. res = 1;
  579. }
  580. spin_unlock_bh(&seq->lock);
  581. exit:
  582. read_unlock_bh(&tipc_nametbl_lock);
  583. return res;
  584. }
  585. /*
  586. * tipc_nametbl_publish - add name publication to network name tables
  587. */
  588. struct publication *tipc_nametbl_publish(u32 type, u32 lower, u32 upper,
  589. u32 scope, u32 port_ref, u32 key)
  590. {
  591. struct publication *publ;
  592. if (table.local_publ_count >= TIPC_MAX_PUBLICATIONS) {
  593. pr_warn("Publication failed, local publication limit reached (%u)\n",
  594. TIPC_MAX_PUBLICATIONS);
  595. return NULL;
  596. }
  597. write_lock_bh(&tipc_nametbl_lock);
  598. publ = tipc_nametbl_insert_publ(type, lower, upper, scope,
  599. tipc_own_addr, port_ref, key);
  600. if (likely(publ)) {
  601. table.local_publ_count++;
  602. tipc_named_publish(publ);
  603. }
  604. write_unlock_bh(&tipc_nametbl_lock);
  605. return publ;
  606. }
  607. /**
  608. * tipc_nametbl_withdraw - withdraw name publication from network name tables
  609. */
  610. int tipc_nametbl_withdraw(u32 type, u32 lower, u32 ref, u32 key)
  611. {
  612. struct publication *publ;
  613. write_lock_bh(&tipc_nametbl_lock);
  614. publ = tipc_nametbl_remove_publ(type, lower, tipc_own_addr, ref, key);
  615. if (likely(publ)) {
  616. table.local_publ_count--;
  617. tipc_named_withdraw(publ);
  618. write_unlock_bh(&tipc_nametbl_lock);
  619. list_del_init(&publ->pport_list);
  620. kfree(publ);
  621. return 1;
  622. }
  623. write_unlock_bh(&tipc_nametbl_lock);
  624. pr_err("Unable to remove local publication\n"
  625. "(type=%u, lower=%u, ref=%u, key=%u)\n",
  626. type, lower, ref, key);
  627. return 0;
  628. }
  629. /**
  630. * tipc_nametbl_subscribe - add a subscription object to the name table
  631. */
  632. void tipc_nametbl_subscribe(struct tipc_subscription *s)
  633. {
  634. u32 type = s->seq.type;
  635. struct name_seq *seq;
  636. write_lock_bh(&tipc_nametbl_lock);
  637. seq = nametbl_find_seq(type);
  638. if (!seq)
  639. seq = tipc_nameseq_create(type, &table.types[hash(type)]);
  640. if (seq) {
  641. spin_lock_bh(&seq->lock);
  642. tipc_nameseq_subscribe(seq, s);
  643. spin_unlock_bh(&seq->lock);
  644. } else {
  645. pr_warn("Failed to create subscription for {%u,%u,%u}\n",
  646. s->seq.type, s->seq.lower, s->seq.upper);
  647. }
  648. write_unlock_bh(&tipc_nametbl_lock);
  649. }
  650. /**
  651. * tipc_nametbl_unsubscribe - remove a subscription object from name table
  652. */
  653. void tipc_nametbl_unsubscribe(struct tipc_subscription *s)
  654. {
  655. struct name_seq *seq;
  656. write_lock_bh(&tipc_nametbl_lock);
  657. seq = nametbl_find_seq(s->seq.type);
  658. if (seq != NULL) {
  659. spin_lock_bh(&seq->lock);
  660. list_del_init(&s->nameseq_list);
  661. spin_unlock_bh(&seq->lock);
  662. nameseq_delete_empty(seq);
  663. }
  664. write_unlock_bh(&tipc_nametbl_lock);
  665. }
  666. /**
  667. * subseq_list - print specified sub-sequence contents into the given buffer
  668. */
  669. static int subseq_list(struct sub_seq *sseq, char *buf, int len, u32 depth,
  670. u32 index)
  671. {
  672. char portIdStr[27];
  673. const char *scope_str[] = {"", " zone", " cluster", " node"};
  674. struct publication *publ;
  675. struct name_info *info;
  676. int ret;
  677. ret = tipc_snprintf(buf, len, "%-10u %-10u ", sseq->lower, sseq->upper);
  678. if (depth == 2) {
  679. ret += tipc_snprintf(buf - ret, len + ret, "\n");
  680. return ret;
  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. ret += tipc_snprintf(buf + ret, len - ret, "%-26s ", portIdStr);
  688. if (depth > 3) {
  689. ret += tipc_snprintf(buf + ret, len - ret, "%-10u %s",
  690. publ->key, scope_str[publ->scope]);
  691. }
  692. if (!list_is_last(&publ->zone_list, &info->zone_list))
  693. ret += tipc_snprintf(buf + ret, len - ret,
  694. "\n%33s", " ");
  695. }
  696. ret += tipc_snprintf(buf + ret, len - ret, "\n");
  697. return ret;
  698. }
  699. /**
  700. * nameseq_list - print specified name sequence contents into the given buffer
  701. */
  702. static int nameseq_list(struct name_seq *seq, char *buf, int len, u32 depth,
  703. u32 type, u32 lowbound, u32 upbound, u32 index)
  704. {
  705. struct sub_seq *sseq;
  706. char typearea[11];
  707. int ret = 0;
  708. if (seq->first_free == 0)
  709. return 0;
  710. sprintf(typearea, "%-10u", seq->type);
  711. if (depth == 1) {
  712. ret += tipc_snprintf(buf, len, "%s\n", typearea);
  713. return ret;
  714. }
  715. for (sseq = seq->sseqs; sseq != &seq->sseqs[seq->first_free]; sseq++) {
  716. if ((lowbound <= sseq->upper) && (upbound >= sseq->lower)) {
  717. ret += tipc_snprintf(buf + ret, len - ret, "%s ",
  718. typearea);
  719. spin_lock_bh(&seq->lock);
  720. ret += subseq_list(sseq, buf + ret, len - ret,
  721. depth, index);
  722. spin_unlock_bh(&seq->lock);
  723. sprintf(typearea, "%10s", " ");
  724. }
  725. }
  726. return ret;
  727. }
  728. /**
  729. * nametbl_header - print name table header into the given buffer
  730. */
  731. static int nametbl_header(char *buf, int len, u32 depth)
  732. {
  733. const char *header[] = {
  734. "Type ",
  735. "Lower Upper ",
  736. "Port Identity ",
  737. "Publication Scope"
  738. };
  739. int i;
  740. int ret = 0;
  741. if (depth > 4)
  742. depth = 4;
  743. for (i = 0; i < depth; i++)
  744. ret += tipc_snprintf(buf + ret, len - ret, header[i]);
  745. ret += tipc_snprintf(buf + ret, len - ret, "\n");
  746. return ret;
  747. }
  748. /**
  749. * nametbl_list - print specified name table contents into the given buffer
  750. */
  751. static int nametbl_list(char *buf, int len, u32 depth_info,
  752. u32 type, u32 lowbound, u32 upbound)
  753. {
  754. struct hlist_head *seq_head;
  755. struct name_seq *seq;
  756. int all_types;
  757. int ret = 0;
  758. u32 depth;
  759. u32 i;
  760. all_types = (depth_info & TIPC_NTQ_ALLTYPES);
  761. depth = (depth_info & ~TIPC_NTQ_ALLTYPES);
  762. if (depth == 0)
  763. return 0;
  764. if (all_types) {
  765. /* display all entries in name table to specified depth */
  766. ret += nametbl_header(buf, len, depth);
  767. lowbound = 0;
  768. upbound = ~0;
  769. for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
  770. seq_head = &table.types[i];
  771. hlist_for_each_entry(seq, seq_head, ns_list) {
  772. ret += nameseq_list(seq, buf + ret, len - ret,
  773. depth, seq->type,
  774. lowbound, upbound, i);
  775. }
  776. }
  777. } else {
  778. /* display only the sequence that matches the specified type */
  779. if (upbound < lowbound) {
  780. ret += tipc_snprintf(buf + ret, len - ret,
  781. "invalid name sequence specified\n");
  782. return ret;
  783. }
  784. ret += nametbl_header(buf + ret, len - ret, depth);
  785. i = hash(type);
  786. seq_head = &table.types[i];
  787. hlist_for_each_entry(seq, seq_head, ns_list) {
  788. if (seq->type == type) {
  789. ret += nameseq_list(seq, buf + ret, len - ret,
  790. depth, type,
  791. lowbound, upbound, i);
  792. break;
  793. }
  794. }
  795. }
  796. return ret;
  797. }
  798. struct sk_buff *tipc_nametbl_get(const void *req_tlv_area, int req_tlv_space)
  799. {
  800. struct sk_buff *buf;
  801. struct tipc_name_table_query *argv;
  802. struct tlv_desc *rep_tlv;
  803. char *pb;
  804. int pb_len;
  805. int str_len;
  806. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NAME_TBL_QUERY))
  807. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  808. buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
  809. if (!buf)
  810. return NULL;
  811. rep_tlv = (struct tlv_desc *)buf->data;
  812. pb = TLV_DATA(rep_tlv);
  813. pb_len = ULTRA_STRING_MAX_LEN;
  814. argv = (struct tipc_name_table_query *)TLV_DATA(req_tlv_area);
  815. read_lock_bh(&tipc_nametbl_lock);
  816. str_len = nametbl_list(pb, pb_len, ntohl(argv->depth),
  817. ntohl(argv->type),
  818. ntohl(argv->lowbound), ntohl(argv->upbound));
  819. read_unlock_bh(&tipc_nametbl_lock);
  820. str_len += 1; /* for "\0" */
  821. skb_put(buf, TLV_SPACE(str_len));
  822. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  823. return buf;
  824. }
  825. int tipc_nametbl_init(void)
  826. {
  827. table.types = kcalloc(TIPC_NAMETBL_SIZE, sizeof(struct hlist_head),
  828. GFP_ATOMIC);
  829. if (!table.types)
  830. return -ENOMEM;
  831. table.local_publ_count = 0;
  832. return 0;
  833. }
  834. void tipc_nametbl_stop(void)
  835. {
  836. u32 i;
  837. if (!table.types)
  838. return;
  839. /* Verify name table is empty, then release it */
  840. write_lock_bh(&tipc_nametbl_lock);
  841. for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
  842. if (hlist_empty(&table.types[i]))
  843. continue;
  844. pr_err("nametbl_stop(): orphaned hash chain detected\n");
  845. break;
  846. }
  847. kfree(table.types);
  848. table.types = NULL;
  849. write_unlock_bh(&tipc_nametbl_lock);
  850. }