name_table.c 26 KB

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