blob: 3a8de4334da15905adb23e9c84f72bdb58c01fed [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/name_table.c: TIPC name table code
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Per Liden593a5f22006-01-11 19:14:19 +01004 * Copyright (c) 2000-2006, Ericsson AB
Allan Stephens968edbe2008-07-14 22:45:33 -07005 * Copyright (c) 2004-2008, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 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.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * 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
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
38#include "config.h"
39#include "dbg.h"
40#include "name_table.h"
41#include "name_distr.h"
42#include "addr.h"
43#include "node_subscr.h"
44#include "subscr.h"
45#include "port.h"
46#include "cluster.h"
47#include "bcast.h"
48
Adrian Bunk988f0882006-03-20 22:37:52 -080049static int tipc_nametbl_size = 1024; /* must be a power of 2 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010050
51/**
52 * struct sub_seq - container for all published instances of a name sequence
53 * @lower: name sequence lower bound
54 * @upper: name sequence upper bound
Allan Stephens968edbe2008-07-14 22:45:33 -070055 * @node_list: circular list of publications made by own node
56 * @cluster_list: circular list of publications made by own cluster
57 * @zone_list: circular list of publications made by own zone
58 * @node_list_size: number of entries in "node_list"
59 * @cluster_list_size: number of entries in "cluster_list"
60 * @zone_list_size: number of entries in "zone_list"
61 *
62 * Note: The zone list always contains at least one entry, since all
63 * publications of the associated name sequence belong to it.
64 * (The cluster and node lists may be empty.)
Per Lidenb97bf3f2006-01-02 19:04:38 +010065 */
66
67struct sub_seq {
68 u32 lower;
69 u32 upper;
70 struct publication *node_list;
71 struct publication *cluster_list;
72 struct publication *zone_list;
Allan Stephens968edbe2008-07-14 22:45:33 -070073 u32 node_list_size;
74 u32 cluster_list_size;
75 u32 zone_list_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +010076};
77
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090078/**
Per Lidenb97bf3f2006-01-02 19:04:38 +010079 * struct name_seq - container for all published instances of a name type
80 * @type: 32 bit 'type' value for name sequence
81 * @sseq: pointer to dynamically-sized array of sub-sequences of this 'type';
82 * sub-sequences are sorted in ascending order
83 * @alloc: number of sub-sequences currently in array
Allan Stephensf1310722006-06-25 23:51:37 -070084 * @first_free: array index of first unused sub-sequence entry
Per Lidenb97bf3f2006-01-02 19:04:38 +010085 * @ns_list: links to adjacent name sequences in hash chain
86 * @subscriptions: list of subscriptions for this 'type'
Allan Stephens307fdf52008-06-04 17:38:22 -070087 * @lock: spinlock controlling access to publication lists of all sub-sequences
Per Lidenb97bf3f2006-01-02 19:04:38 +010088 */
89
90struct name_seq {
91 u32 type;
92 struct sub_seq *sseqs;
93 u32 alloc;
94 u32 first_free;
95 struct hlist_node ns_list;
96 struct list_head subscriptions;
97 spinlock_t lock;
98};
99
100/**
101 * struct name_table - table containing all existing port name publications
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900102 * @types: pointer to fixed-sized array of name sequence lists,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100103 * accessed via hashing on 'type'; name sequence lists are *not* sorted
104 * @local_publ_count: number of publications issued by this node
105 */
106
107struct name_table {
108 struct hlist_head *types;
109 u32 local_publ_count;
110};
111
Per Liden4323add2006-01-18 00:38:21 +0100112static struct name_table table = { NULL } ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100113static atomic_t rsv_publ_ok = ATOMIC_INIT(0);
Ingo Molnar34af9462006-06-27 02:53:55 -0700114DEFINE_RWLOCK(tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100115
116
Sam Ravnborg05790c62006-03-20 22:37:04 -0800117static int hash(int x)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100118{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000119 return x & (tipc_nametbl_size - 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120}
121
122/**
123 * publ_create - create a publication structure
124 */
125
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900126static struct publication *publ_create(u32 type, u32 lower, u32 upper,
127 u32 scope, u32 node, u32 port_ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128 u32 key)
129{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700130 struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100131 if (publ == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700132 warn("Publication creation failure, no memory\n");
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800133 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100134 }
135
Per Lidenb97bf3f2006-01-02 19:04:38 +0100136 publ->type = type;
137 publ->lower = lower;
138 publ->upper = upper;
139 publ->scope = scope;
140 publ->node = node;
141 publ->ref = port_ref;
142 publ->key = key;
143 INIT_LIST_HEAD(&publ->local_list);
144 INIT_LIST_HEAD(&publ->pport_list);
145 INIT_LIST_HEAD(&publ->subscr.nodesub_list);
146 return publ;
147}
148
149/**
Per Liden4323add2006-01-18 00:38:21 +0100150 * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
Per Lidenb97bf3f2006-01-02 19:04:38 +0100151 */
152
Adrian Bunk988f0882006-03-20 22:37:52 -0800153static struct sub_seq *tipc_subseq_alloc(u32 cnt)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100154{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700155 struct sub_seq *sseq = kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156 return sseq;
157}
158
159/**
Per Liden4323add2006-01-18 00:38:21 +0100160 * tipc_nameseq_create - create a name sequence structure for the specified 'type'
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900161 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100162 * Allocates a single sub-sequence structure and sets it to all 0's.
163 */
164
Adrian Bunk988f0882006-03-20 22:37:52 -0800165static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700167 struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC);
Per Liden4323add2006-01-18 00:38:21 +0100168 struct sub_seq *sseq = tipc_subseq_alloc(1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169
170 if (!nseq || !sseq) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700171 warn("Name sequence creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100172 kfree(nseq);
173 kfree(sseq);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800174 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100175 }
176
Ingo Molnar34af9462006-06-27 02:53:55 -0700177 spin_lock_init(&nseq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178 nseq->type = type;
179 nseq->sseqs = sseq;
Allan Stephensf1310722006-06-25 23:51:37 -0700180 dbg("tipc_nameseq_create(): nseq = %p, type %u, ssseqs %p, ff: %u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181 nseq, type, nseq->sseqs, nseq->first_free);
182 nseq->alloc = 1;
183 INIT_HLIST_NODE(&nseq->ns_list);
184 INIT_LIST_HEAD(&nseq->subscriptions);
185 hlist_add_head(&nseq->ns_list, seq_head);
186 return nseq;
187}
188
189/**
190 * nameseq_find_subseq - find sub-sequence (if any) matching a name instance
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900191 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100192 * Very time-critical, so binary searches through sub-sequence array.
193 */
194
Sam Ravnborg05790c62006-03-20 22:37:04 -0800195static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq,
196 u32 instance)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100197{
198 struct sub_seq *sseqs = nseq->sseqs;
199 int low = 0;
200 int high = nseq->first_free - 1;
201 int mid;
202
203 while (low <= high) {
204 mid = (low + high) / 2;
205 if (instance < sseqs[mid].lower)
206 high = mid - 1;
207 else if (instance > sseqs[mid].upper)
208 low = mid + 1;
209 else
210 return &sseqs[mid];
211 }
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800212 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100213}
214
215/**
216 * nameseq_locate_subseq - determine position of name instance in sub-sequence
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900217 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100218 * Returns index in sub-sequence array of the entry that contains the specified
219 * instance value; if no entry contains that value, returns the position
220 * where a new entry for it would be inserted in the array.
221 *
222 * Note: Similar to binary search code for locating a sub-sequence.
223 */
224
225static u32 nameseq_locate_subseq(struct name_seq *nseq, u32 instance)
226{
227 struct sub_seq *sseqs = nseq->sseqs;
228 int low = 0;
229 int high = nseq->first_free - 1;
230 int mid;
231
232 while (low <= high) {
233 mid = (low + high) / 2;
234 if (instance < sseqs[mid].lower)
235 high = mid - 1;
236 else if (instance > sseqs[mid].upper)
237 low = mid + 1;
238 else
239 return mid;
240 }
241 return low;
242}
243
244/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900245 * tipc_nameseq_insert_publ -
Per Lidenb97bf3f2006-01-02 19:04:38 +0100246 */
247
Adrian Bunk988f0882006-03-20 22:37:52 -0800248static struct publication *tipc_nameseq_insert_publ(struct name_seq *nseq,
249 u32 type, u32 lower, u32 upper,
250 u32 scope, u32 node, u32 port, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100251{
252 struct subscription *s;
253 struct subscription *st;
254 struct publication *publ;
255 struct sub_seq *sseq;
256 int created_subseq = 0;
257
Per Lidenb97bf3f2006-01-02 19:04:38 +0100258 sseq = nameseq_find_subseq(nseq, lower);
Allan Stephensf1310722006-06-25 23:51:37 -0700259 dbg("nameseq_ins: for seq %p, {%u,%u}, found sseq %p\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100260 nseq, type, lower, sseq);
261 if (sseq) {
262
263 /* Lower end overlaps existing entry => need an exact match */
264
265 if ((sseq->lower != lower) || (sseq->upper != upper)) {
Allan Stephensf1310722006-06-25 23:51:37 -0700266 warn("Cannot publish {%u,%u,%u}, overlap error\n",
267 type, lower, upper);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800268 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100269 }
270 } else {
271 u32 inspos;
272 struct sub_seq *freesseq;
273
274 /* Find where lower end should be inserted */
275
276 inspos = nameseq_locate_subseq(nseq, lower);
277
278 /* Fail if upper end overlaps into an existing entry */
279
280 if ((inspos < nseq->first_free) &&
281 (upper >= nseq->sseqs[inspos].lower)) {
Allan Stephensf1310722006-06-25 23:51:37 -0700282 warn("Cannot publish {%u,%u,%u}, overlap error\n",
283 type, lower, upper);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800284 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100285 }
286
287 /* Ensure there is space for new sub-sequence */
288
289 if (nseq->first_free == nseq->alloc) {
Allan Stephens9ab230f2006-06-25 23:37:24 -0700290 struct sub_seq *sseqs = tipc_subseq_alloc(nseq->alloc * 2);
291
292 if (!sseqs) {
Allan Stephensf1310722006-06-25 23:51:37 -0700293 warn("Cannot publish {%u,%u,%u}, no memory\n",
294 type, lower, upper);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800295 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100296 }
Allan Stephens9ab230f2006-06-25 23:37:24 -0700297 dbg("Allocated %u more sseqs\n", nseq->alloc);
298 memcpy(sseqs, nseq->sseqs,
299 nseq->alloc * sizeof(struct sub_seq));
300 kfree(nseq->sseqs);
301 nseq->sseqs = sseqs;
302 nseq->alloc *= 2;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100303 }
304 dbg("Have %u sseqs for type %u\n", nseq->alloc, type);
305
306 /* Insert new sub-sequence */
307
308 dbg("ins in pos %u, ff = %u\n", inspos, nseq->first_free);
309 sseq = &nseq->sseqs[inspos];
310 freesseq = &nseq->sseqs[nseq->first_free];
311 memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof (*sseq));
312 memset(sseq, 0, sizeof (*sseq));
313 nseq->first_free++;
314 sseq->lower = lower;
315 sseq->upper = upper;
316 created_subseq = 1;
317 }
Allan Stephensf1310722006-06-25 23:51:37 -0700318 dbg("inserting {%u,%u,%u} from <0x%x:%u> into sseq %p(%u,%u) of seq %p\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100319 type, lower, upper, node, port, sseq,
320 sseq->lower, sseq->upper, nseq);
321
322 /* Insert a publication: */
323
324 publ = publ_create(type, lower, upper, scope, node, port, key);
325 if (!publ)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800326 return NULL;
Allan Stephensf1310722006-06-25 23:51:37 -0700327 dbg("inserting publ %p, node=0x%x publ->node=0x%x, subscr->node=%p\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100328 publ, node, publ->node, publ->subscr.node);
329
Allan Stephens968edbe2008-07-14 22:45:33 -0700330 sseq->zone_list_size++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100331 if (!sseq->zone_list)
332 sseq->zone_list = publ->zone_list_next = publ;
333 else {
334 publ->zone_list_next = sseq->zone_list->zone_list_next;
335 sseq->zone_list->zone_list_next = publ;
336 }
337
338 if (in_own_cluster(node)) {
Allan Stephens968edbe2008-07-14 22:45:33 -0700339 sseq->cluster_list_size++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100340 if (!sseq->cluster_list)
341 sseq->cluster_list = publ->cluster_list_next = publ;
342 else {
343 publ->cluster_list_next =
344 sseq->cluster_list->cluster_list_next;
345 sseq->cluster_list->cluster_list_next = publ;
346 }
347 }
348
349 if (node == tipc_own_addr) {
Allan Stephens968edbe2008-07-14 22:45:33 -0700350 sseq->node_list_size++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351 if (!sseq->node_list)
352 sseq->node_list = publ->node_list_next = publ;
353 else {
354 publ->node_list_next = sseq->node_list->node_list_next;
355 sseq->node_list->node_list_next = publ;
356 }
357 }
358
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900359 /*
360 * Any subscriptions waiting for notification?
Per Lidenb97bf3f2006-01-02 19:04:38 +0100361 */
362 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
363 dbg("calling report_overlap()\n");
Per Liden4323add2006-01-18 00:38:21 +0100364 tipc_subscr_report_overlap(s,
365 publ->lower,
366 publ->upper,
367 TIPC_PUBLISHED,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900368 publ->ref,
Per Liden4323add2006-01-18 00:38:21 +0100369 publ->node,
370 created_subseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100371 }
372 return publ;
373}
374
375/**
Per Liden4323add2006-01-18 00:38:21 +0100376 * tipc_nameseq_remove_publ -
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900377 *
Allan Stephensf1310722006-06-25 23:51:37 -0700378 * NOTE: There may be cases where TIPC is asked to remove a publication
379 * that is not in the name table. For example, if another node issues a
380 * publication for a name sequence that overlaps an existing name sequence
381 * the publication will not be recorded, which means the publication won't
382 * be found when the name sequence is later withdrawn by that node.
383 * A failed withdraw request simply returns a failure indication and lets the
384 * caller issue any error or warning messages associated with such a problem.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385 */
386
Adrian Bunk988f0882006-03-20 22:37:52 -0800387static struct publication *tipc_nameseq_remove_publ(struct name_seq *nseq, u32 inst,
388 u32 node, u32 ref, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100389{
390 struct publication *publ;
Allan Stephensf1310722006-06-25 23:51:37 -0700391 struct publication *curr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392 struct publication *prev;
393 struct sub_seq *sseq = nameseq_find_subseq(nseq, inst);
394 struct sub_seq *free;
395 struct subscription *s, *st;
396 int removed_subseq = 0;
397
Allan Stephensf1310722006-06-25 23:51:37 -0700398 if (!sseq)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800399 return NULL;
Allan Stephensf1310722006-06-25 23:51:37 -0700400
401 dbg("tipc_nameseq_remove_publ: seq: %p, sseq %p, {%u,%u}, key %u\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100402 nseq, sseq, nseq->type, inst, key);
403
Allan Stephensf1310722006-06-25 23:51:37 -0700404 /* Remove publication from zone scope list */
405
Per Lidenb97bf3f2006-01-02 19:04:38 +0100406 prev = sseq->zone_list;
407 publ = sseq->zone_list->zone_list_next;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900408 while ((publ->key != key) || (publ->ref != ref) ||
Per Lidenb97bf3f2006-01-02 19:04:38 +0100409 (publ->node && (publ->node != node))) {
410 prev = publ;
411 publ = publ->zone_list_next;
Allan Stephensf1310722006-06-25 23:51:37 -0700412 if (prev == sseq->zone_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900413
Allan Stephensf1310722006-06-25 23:51:37 -0700414 /* Prevent endless loop if publication not found */
415
416 return NULL;
417 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100418 }
419 if (publ != sseq->zone_list)
420 prev->zone_list_next = publ->zone_list_next;
421 else if (publ->zone_list_next != publ) {
422 prev->zone_list_next = publ->zone_list_next;
423 sseq->zone_list = publ->zone_list_next;
424 } else {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800425 sseq->zone_list = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426 }
Allan Stephens968edbe2008-07-14 22:45:33 -0700427 sseq->zone_list_size--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100428
Allan Stephensf1310722006-06-25 23:51:37 -0700429 /* Remove publication from cluster scope list, if present */
430
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431 if (in_own_cluster(node)) {
432 prev = sseq->cluster_list;
Allan Stephensf1310722006-06-25 23:51:37 -0700433 curr = sseq->cluster_list->cluster_list_next;
434 while (curr != publ) {
435 prev = curr;
436 curr = curr->cluster_list_next;
437 if (prev == sseq->cluster_list) {
438
439 /* Prevent endless loop for malformed list */
440
441 err("Unable to de-list cluster publication\n"
442 "{%u%u}, node=0x%x, ref=%u, key=%u)\n",
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900443 publ->type, publ->lower, publ->node,
Allan Stephensf1310722006-06-25 23:51:37 -0700444 publ->ref, publ->key);
445 goto end_cluster;
446 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100447 }
448 if (publ != sseq->cluster_list)
449 prev->cluster_list_next = publ->cluster_list_next;
450 else if (publ->cluster_list_next != publ) {
451 prev->cluster_list_next = publ->cluster_list_next;
452 sseq->cluster_list = publ->cluster_list_next;
453 } else {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800454 sseq->cluster_list = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100455 }
Allan Stephens968edbe2008-07-14 22:45:33 -0700456 sseq->cluster_list_size--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457 }
Allan Stephensf1310722006-06-25 23:51:37 -0700458end_cluster:
459
460 /* Remove publication from node scope list, if present */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100461
462 if (node == tipc_own_addr) {
463 prev = sseq->node_list;
Allan Stephensf1310722006-06-25 23:51:37 -0700464 curr = sseq->node_list->node_list_next;
465 while (curr != publ) {
466 prev = curr;
467 curr = curr->node_list_next;
468 if (prev == sseq->node_list) {
469
470 /* Prevent endless loop for malformed list */
471
472 err("Unable to de-list node publication\n"
473 "{%u%u}, node=0x%x, ref=%u, key=%u)\n",
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900474 publ->type, publ->lower, publ->node,
Allan Stephensf1310722006-06-25 23:51:37 -0700475 publ->ref, publ->key);
476 goto end_node;
477 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100478 }
479 if (publ != sseq->node_list)
480 prev->node_list_next = publ->node_list_next;
481 else if (publ->node_list_next != publ) {
482 prev->node_list_next = publ->node_list_next;
483 sseq->node_list = publ->node_list_next;
484 } else {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800485 sseq->node_list = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100486 }
Allan Stephens968edbe2008-07-14 22:45:33 -0700487 sseq->node_list_size--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488 }
Allan Stephensf1310722006-06-25 23:51:37 -0700489end_node:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100490
Allan Stephensf1310722006-06-25 23:51:37 -0700491 /* Contract subseq list if no more publications for that subseq */
492
493 if (!sseq->zone_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100494 free = &nseq->sseqs[nseq->first_free--];
495 memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof (*sseq));
496 removed_subseq = 1;
497 }
498
Allan Stephensf1310722006-06-25 23:51:37 -0700499 /* Notify any waiting subscriptions */
500
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
Per Liden4323add2006-01-18 00:38:21 +0100502 tipc_subscr_report_overlap(s,
503 publ->lower,
504 publ->upper,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900505 TIPC_WITHDRAWN,
506 publ->ref,
Per Liden4323add2006-01-18 00:38:21 +0100507 publ->node,
508 removed_subseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509 }
Allan Stephensf1310722006-06-25 23:51:37 -0700510
Per Lidenb97bf3f2006-01-02 19:04:38 +0100511 return publ;
512}
513
514/**
Per Liden4323add2006-01-18 00:38:21 +0100515 * tipc_nameseq_subscribe: attach a subscription, and issue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100516 * the prescribed number of events if there is any sub-
517 * sequence overlapping with the requested sequence
518 */
519
Florian Westphal248bbf32007-08-02 19:26:23 -0700520static void tipc_nameseq_subscribe(struct name_seq *nseq, struct subscription *s)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100521{
522 struct sub_seq *sseq = nseq->sseqs;
523
524 list_add(&s->nameseq_list, &nseq->subscriptions);
525
526 if (!sseq)
527 return;
528
529 while (sseq != &nseq->sseqs[nseq->first_free]) {
530 struct publication *zl = sseq->zone_list;
Per Liden4323add2006-01-18 00:38:21 +0100531 if (zl && tipc_subscr_overlap(s,sseq->lower,sseq->upper)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100532 struct publication *crs = zl;
533 int must_report = 1;
534
535 do {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900536 tipc_subscr_report_overlap(s,
537 sseq->lower,
Per Liden4323add2006-01-18 00:38:21 +0100538 sseq->upper,
539 TIPC_PUBLISHED,
540 crs->ref,
541 crs->node,
542 must_report);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543 must_report = 0;
544 crs = crs->zone_list_next;
545 } while (crs != zl);
546 }
547 sseq++;
548 }
549}
550
551static struct name_seq *nametbl_find_seq(u32 type)
552{
553 struct hlist_head *seq_head;
554 struct hlist_node *seq_node;
555 struct name_seq *ns;
556
557 dbg("find_seq %u,(%u,0x%x) table = %p, hash[type] = %u\n",
Harvey Harrisonf5741792008-11-07 23:37:50 -0800558 type, htonl(type), type, table.types, hash(type));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100559
560 seq_head = &table.types[hash(type)];
561 hlist_for_each_entry(ns, seq_node, seq_head, ns_list) {
562 if (ns->type == type) {
Allan Stephensf1310722006-06-25 23:51:37 -0700563 dbg("found %p\n", ns);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100564 return ns;
565 }
566 }
567
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800568 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100569};
570
Per Liden4323add2006-01-18 00:38:21 +0100571struct publication *tipc_nametbl_insert_publ(u32 type, u32 lower, u32 upper,
572 u32 scope, u32 node, u32 port, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100573{
574 struct name_seq *seq = nametbl_find_seq(type);
575
Allan Stephensf1310722006-06-25 23:51:37 -0700576 dbg("tipc_nametbl_insert_publ: {%u,%u,%u} found %p\n", type, lower, upper, seq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100577 if (lower > upper) {
Allan Stephensf1310722006-06-25 23:51:37 -0700578 warn("Failed to publish illegal {%u,%u,%u}\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100579 type, lower, upper);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800580 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100581 }
582
Allan Stephensf1310722006-06-25 23:51:37 -0700583 dbg("Publishing {%u,%u,%u} from 0x%x\n", type, lower, upper, node);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100584 if (!seq) {
Per Liden4323add2006-01-18 00:38:21 +0100585 seq = tipc_nameseq_create(type, &table.types[hash(type)]);
Allan Stephensf1310722006-06-25 23:51:37 -0700586 dbg("tipc_nametbl_insert_publ: created %p\n", seq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100587 }
588 if (!seq)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800589 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100590
Per Liden4323add2006-01-18 00:38:21 +0100591 return tipc_nameseq_insert_publ(seq, type, lower, upper,
592 scope, node, port, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100593}
594
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900595struct publication *tipc_nametbl_remove_publ(u32 type, u32 lower,
Per Liden4323add2006-01-18 00:38:21 +0100596 u32 node, u32 ref, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100597{
598 struct publication *publ;
599 struct name_seq *seq = nametbl_find_seq(type);
600
601 if (!seq)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800602 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100603
Allan Stephensf1310722006-06-25 23:51:37 -0700604 dbg("Withdrawing {%u,%u} from 0x%x\n", type, lower, node);
Per Liden4323add2006-01-18 00:38:21 +0100605 publ = tipc_nameseq_remove_publ(seq, lower, node, ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606
607 if (!seq->first_free && list_empty(&seq->subscriptions)) {
608 hlist_del_init(&seq->ns_list);
609 kfree(seq->sseqs);
610 kfree(seq);
611 }
612 return publ;
613}
614
615/*
Allan Stephens5d9c54c2010-09-03 08:33:39 +0000616 * tipc_nametbl_translate - translate name to port id
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617 *
618 * Note: on entry 'destnode' is the search domain used during translation;
619 * on exit it passes back the node address of the matching port (if any)
620 */
621
Per Liden4323add2006-01-18 00:38:21 +0100622u32 tipc_nametbl_translate(u32 type, u32 instance, u32 *destnode)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100623{
624 struct sub_seq *sseq;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800625 struct publication *publ = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100626 struct name_seq *seq;
627 u32 ref;
628
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000629 if (!tipc_in_scope(*destnode, tipc_own_addr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630 return 0;
631
Per Liden4323add2006-01-18 00:38:21 +0100632 read_lock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633 seq = nametbl_find_seq(type);
634 if (unlikely(!seq))
635 goto not_found;
636 sseq = nameseq_find_subseq(seq, instance);
637 if (unlikely(!sseq))
638 goto not_found;
639 spin_lock_bh(&seq->lock);
640
641 /* Closest-First Algorithm: */
642 if (likely(!*destnode)) {
643 publ = sseq->node_list;
644 if (publ) {
645 sseq->node_list = publ->node_list_next;
646found:
647 ref = publ->ref;
648 *destnode = publ->node;
649 spin_unlock_bh(&seq->lock);
Per Liden4323add2006-01-18 00:38:21 +0100650 read_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651 return ref;
652 }
653 publ = sseq->cluster_list;
654 if (publ) {
655 sseq->cluster_list = publ->cluster_list_next;
656 goto found;
657 }
658 publ = sseq->zone_list;
659 if (publ) {
660 sseq->zone_list = publ->zone_list_next;
661 goto found;
662 }
663 }
664
665 /* Round-Robin Algorithm: */
666 else if (*destnode == tipc_own_addr) {
667 publ = sseq->node_list;
668 if (publ) {
669 sseq->node_list = publ->node_list_next;
670 goto found;
671 }
672 } else if (in_own_cluster(*destnode)) {
673 publ = sseq->cluster_list;
674 if (publ) {
675 sseq->cluster_list = publ->cluster_list_next;
676 goto found;
677 }
678 } else {
679 publ = sseq->zone_list;
680 if (publ) {
681 sseq->zone_list = publ->zone_list_next;
682 goto found;
683 }
684 }
685 spin_unlock_bh(&seq->lock);
686not_found:
Per Liden4323add2006-01-18 00:38:21 +0100687 read_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100688 return 0;
689}
690
691/**
Per Liden4323add2006-01-18 00:38:21 +0100692 * tipc_nametbl_mc_translate - find multicast destinations
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900693 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100694 * Creates list of all local ports that overlap the given multicast address;
695 * also determines if any off-node ports overlap.
696 *
697 * Note: Publications with a scope narrower than 'limit' are ignored.
698 * (i.e. local node-scope publications mustn't receive messages arriving
699 * from another node, even if the multcast link brought it here)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900700 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100701 * Returns non-zero if any off-node ports overlap
702 */
703
Per Liden4323add2006-01-18 00:38:21 +0100704int tipc_nametbl_mc_translate(u32 type, u32 lower, u32 upper, u32 limit,
705 struct port_list *dports)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100706{
707 struct name_seq *seq;
708 struct sub_seq *sseq;
709 struct sub_seq *sseq_stop;
710 int res = 0;
711
Per Liden4323add2006-01-18 00:38:21 +0100712 read_lock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100713 seq = nametbl_find_seq(type);
714 if (!seq)
715 goto exit;
716
717 spin_lock_bh(&seq->lock);
718
719 sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
720 sseq_stop = seq->sseqs + seq->first_free;
721 for (; sseq != sseq_stop; sseq++) {
722 struct publication *publ;
723
724 if (sseq->lower > upper)
725 break;
Allan Stephens968edbe2008-07-14 22:45:33 -0700726
727 publ = sseq->node_list;
728 if (publ) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100729 do {
Allan Stephens968edbe2008-07-14 22:45:33 -0700730 if (publ->scope <= limit)
Per Liden4323add2006-01-18 00:38:21 +0100731 tipc_port_list_add(dports, publ->ref);
Allan Stephens968edbe2008-07-14 22:45:33 -0700732 publ = publ->node_list_next;
733 } while (publ != sseq->node_list);
734 }
735
736 if (sseq->cluster_list_size != sseq->node_list_size)
737 res = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100738 }
739
740 spin_unlock_bh(&seq->lock);
741exit:
Per Liden4323add2006-01-18 00:38:21 +0100742 read_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100743 return res;
744}
745
746/**
Per Liden4323add2006-01-18 00:38:21 +0100747 * tipc_nametbl_publish_rsv - publish port name using a reserved name type
Per Lidenb97bf3f2006-01-02 19:04:38 +0100748 */
749
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900750int tipc_nametbl_publish_rsv(u32 ref, unsigned int scope,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100751 struct tipc_name_seq const *seq)
752{
753 int res;
754
755 atomic_inc(&rsv_publ_ok);
756 res = tipc_publish(ref, scope, seq);
757 atomic_dec(&rsv_publ_ok);
758 return res;
759}
760
761/**
Per Liden4323add2006-01-18 00:38:21 +0100762 * tipc_nametbl_publish - add name publication to network name tables
Per Lidenb97bf3f2006-01-02 19:04:38 +0100763 */
764
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900765struct publication *tipc_nametbl_publish(u32 type, u32 lower, u32 upper,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100766 u32 scope, u32 port_ref, u32 key)
767{
768 struct publication *publ;
769
770 if (table.local_publ_count >= tipc_max_publications) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900771 warn("Publication failed, local publication limit reached (%u)\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100772 tipc_max_publications);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800773 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100774 }
775 if ((type < TIPC_RESERVED_TYPES) && !atomic_read(&rsv_publ_ok)) {
Allan Stephensf1310722006-06-25 23:51:37 -0700776 warn("Publication failed, reserved name {%u,%u,%u}\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100777 type, lower, upper);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800778 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100779 }
780
Per Liden4323add2006-01-18 00:38:21 +0100781 write_lock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100782 table.local_publ_count++;
Per Liden4323add2006-01-18 00:38:21 +0100783 publ = tipc_nametbl_insert_publ(type, lower, upper, scope,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100784 tipc_own_addr, port_ref, key);
785 if (publ && (scope != TIPC_NODE_SCOPE)) {
Per Liden4323add2006-01-18 00:38:21 +0100786 tipc_named_publish(publ);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100787 }
Per Liden4323add2006-01-18 00:38:21 +0100788 write_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100789 return publ;
790}
791
792/**
Per Liden4323add2006-01-18 00:38:21 +0100793 * tipc_nametbl_withdraw - withdraw name publication from network name tables
Per Lidenb97bf3f2006-01-02 19:04:38 +0100794 */
795
Per Liden4323add2006-01-18 00:38:21 +0100796int tipc_nametbl_withdraw(u32 type, u32 lower, u32 ref, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100797{
798 struct publication *publ;
799
Allan Stephensf1310722006-06-25 23:51:37 -0700800 dbg("tipc_nametbl_withdraw: {%u,%u}, key=%u\n", type, lower, key);
Per Liden4323add2006-01-18 00:38:21 +0100801 write_lock_bh(&tipc_nametbl_lock);
802 publ = tipc_nametbl_remove_publ(type, lower, tipc_own_addr, ref, key);
Allan Stephensf1310722006-06-25 23:51:37 -0700803 if (likely(publ)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100804 table.local_publ_count--;
805 if (publ->scope != TIPC_NODE_SCOPE)
Per Liden4323add2006-01-18 00:38:21 +0100806 tipc_named_withdraw(publ);
807 write_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100808 list_del_init(&publ->pport_list);
809 kfree(publ);
810 return 1;
811 }
Per Liden4323add2006-01-18 00:38:21 +0100812 write_unlock_bh(&tipc_nametbl_lock);
Allan Stephensf1310722006-06-25 23:51:37 -0700813 err("Unable to remove local publication\n"
814 "(type=%u, lower=%u, ref=%u, key=%u)\n",
815 type, lower, ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100816 return 0;
817}
818
819/**
Per Liden4323add2006-01-18 00:38:21 +0100820 * tipc_nametbl_subscribe - add a subscription object to the name table
Per Lidenb97bf3f2006-01-02 19:04:38 +0100821 */
822
Allan Stephensf1310722006-06-25 23:51:37 -0700823void tipc_nametbl_subscribe(struct subscription *s)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100824{
825 u32 type = s->seq.type;
826 struct name_seq *seq;
827
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900828 write_lock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100829 seq = nametbl_find_seq(type);
830 if (!seq) {
Per Liden4323add2006-01-18 00:38:21 +0100831 seq = tipc_nameseq_create(type, &table.types[hash(type)]);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100832 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900833 if (seq){
834 spin_lock_bh(&seq->lock);
835 dbg("tipc_nametbl_subscribe:found %p for {%u,%u,%u}\n",
836 seq, type, s->seq.lower, s->seq.upper);
837 tipc_nameseq_subscribe(seq, s);
838 spin_unlock_bh(&seq->lock);
839 } else {
Allan Stephensf1310722006-06-25 23:51:37 -0700840 warn("Failed to create subscription for {%u,%u,%u}\n",
841 s->seq.type, s->seq.lower, s->seq.upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900842 }
843 write_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100844}
845
846/**
Per Liden4323add2006-01-18 00:38:21 +0100847 * tipc_nametbl_unsubscribe - remove a subscription object from name table
Per Lidenb97bf3f2006-01-02 19:04:38 +0100848 */
849
Allan Stephensf1310722006-06-25 23:51:37 -0700850void tipc_nametbl_unsubscribe(struct subscription *s)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100851{
852 struct name_seq *seq;
853
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900854 write_lock_bh(&tipc_nametbl_lock);
855 seq = nametbl_find_seq(s->seq.type);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100856 if (seq != NULL){
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900857 spin_lock_bh(&seq->lock);
858 list_del_init(&s->nameseq_list);
859 spin_unlock_bh(&seq->lock);
860 if ((seq->first_free == 0) && list_empty(&seq->subscriptions)) {
861 hlist_del_init(&seq->ns_list);
862 kfree(seq->sseqs);
863 kfree(seq);
864 }
865 }
866 write_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100867}
868
869
870/**
871 * subseq_list: print specified sub-sequence contents into the given buffer
872 */
873
874static void subseq_list(struct sub_seq *sseq, struct print_buf *buf, u32 depth,
875 u32 index)
876{
877 char portIdStr[27];
Allan Stephensc2de5812010-08-17 11:00:14 +0000878 const char *scope_str[] = {"", " zone", " cluster", " node"};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100879 struct publication *publ = sseq->zone_list;
880
881 tipc_printf(buf, "%-10u %-10u ", sseq->lower, sseq->upper);
882
883 if (depth == 2 || !publ) {
884 tipc_printf(buf, "\n");
885 return;
886 }
887
888 do {
889 sprintf (portIdStr, "<%u.%u.%u:%u>",
890 tipc_zone(publ->node), tipc_cluster(publ->node),
891 tipc_node(publ->node), publ->ref);
892 tipc_printf(buf, "%-26s ", portIdStr);
893 if (depth > 3) {
Allan Stephensc2de5812010-08-17 11:00:14 +0000894 tipc_printf(buf, "%-10u %s", publ->key,
895 scope_str[publ->scope]);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100896 }
897
898 publ = publ->zone_list_next;
899 if (publ == sseq->zone_list)
900 break;
901
902 tipc_printf(buf, "\n%33s", " ");
903 } while (1);
904
905 tipc_printf(buf, "\n");
906}
907
908/**
909 * nameseq_list: print specified name sequence contents into the given buffer
910 */
911
912static void nameseq_list(struct name_seq *seq, struct print_buf *buf, u32 depth,
913 u32 type, u32 lowbound, u32 upbound, u32 index)
914{
915 struct sub_seq *sseq;
916 char typearea[11];
917
Allan Stephens0f15d362008-06-04 17:37:59 -0700918 if (seq->first_free == 0)
919 return;
920
Per Lidenb97bf3f2006-01-02 19:04:38 +0100921 sprintf(typearea, "%-10u", seq->type);
922
923 if (depth == 1) {
924 tipc_printf(buf, "%s\n", typearea);
925 return;
926 }
927
928 for (sseq = seq->sseqs; sseq != &seq->sseqs[seq->first_free]; sseq++) {
929 if ((lowbound <= sseq->upper) && (upbound >= sseq->lower)) {
930 tipc_printf(buf, "%s ", typearea);
Allan Stephens307fdf52008-06-04 17:38:22 -0700931 spin_lock_bh(&seq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100932 subseq_list(sseq, buf, depth, index);
Allan Stephens307fdf52008-06-04 17:38:22 -0700933 spin_unlock_bh(&seq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100934 sprintf(typearea, "%10s", " ");
935 }
936 }
937}
938
939/**
940 * nametbl_header - print name table header into the given buffer
941 */
942
943static void nametbl_header(struct print_buf *buf, u32 depth)
944{
Allan Stephensc2de5812010-08-17 11:00:14 +0000945 const char *header[] = {
946 "Type ",
947 "Lower Upper ",
948 "Port Identity ",
949 "Publication Scope"
950 };
Per Lidenb97bf3f2006-01-02 19:04:38 +0100951
Allan Stephensc2de5812010-08-17 11:00:14 +0000952 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100953
Allan Stephensc2de5812010-08-17 11:00:14 +0000954 if (depth > 4)
955 depth = 4;
956 for (i = 0; i < depth; i++)
957 tipc_printf(buf, header[i]);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100958 tipc_printf(buf, "\n");
959}
960
961/**
962 * nametbl_list - print specified name table contents into the given buffer
963 */
964
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900965static void nametbl_list(struct print_buf *buf, u32 depth_info,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100966 u32 type, u32 lowbound, u32 upbound)
967{
968 struct hlist_head *seq_head;
969 struct hlist_node *seq_node;
970 struct name_seq *seq;
971 int all_types;
972 u32 depth;
973 u32 i;
974
975 all_types = (depth_info & TIPC_NTQ_ALLTYPES);
976 depth = (depth_info & ~TIPC_NTQ_ALLTYPES);
977
978 if (depth == 0)
979 return;
980
981 if (all_types) {
982 /* display all entries in name table to specified depth */
983 nametbl_header(buf, depth);
984 lowbound = 0;
985 upbound = ~0;
986 for (i = 0; i < tipc_nametbl_size; i++) {
987 seq_head = &table.types[i];
988 hlist_for_each_entry(seq, seq_node, seq_head, ns_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900989 nameseq_list(seq, buf, depth, seq->type,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100990 lowbound, upbound, i);
991 }
992 }
993 } else {
994 /* display only the sequence that matches the specified type */
995 if (upbound < lowbound) {
996 tipc_printf(buf, "invalid name sequence specified\n");
997 return;
998 }
999 nametbl_header(buf, depth);
1000 i = hash(type);
1001 seq_head = &table.types[i];
1002 hlist_for_each_entry(seq, seq_node, seq_head, ns_list) {
1003 if (seq->type == type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001004 nameseq_list(seq, buf, depth, type,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001005 lowbound, upbound, i);
1006 break;
1007 }
1008 }
1009 }
1010}
1011
Per Lidenb97bf3f2006-01-02 19:04:38 +01001012#define MAX_NAME_TBL_QUERY 32768
1013
Per Liden4323add2006-01-18 00:38:21 +01001014struct sk_buff *tipc_nametbl_get(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001015{
1016 struct sk_buff *buf;
1017 struct tipc_name_table_query *argv;
1018 struct tlv_desc *rep_tlv;
1019 struct print_buf b;
1020 int str_len;
1021
1022 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NAME_TBL_QUERY))
Per Liden4323add2006-01-18 00:38:21 +01001023 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001024
Per Liden4323add2006-01-18 00:38:21 +01001025 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_NAME_TBL_QUERY));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001026 if (!buf)
1027 return NULL;
1028
1029 rep_tlv = (struct tlv_desc *)buf->data;
Per Liden4323add2006-01-18 00:38:21 +01001030 tipc_printbuf_init(&b, TLV_DATA(rep_tlv), MAX_NAME_TBL_QUERY);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001031 argv = (struct tipc_name_table_query *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01001032 read_lock_bh(&tipc_nametbl_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001033 nametbl_list(&b, ntohl(argv->depth), ntohl(argv->type),
Per Lidenb97bf3f2006-01-02 19:04:38 +01001034 ntohl(argv->lowbound), ntohl(argv->upbound));
Per Liden4323add2006-01-18 00:38:21 +01001035 read_unlock_bh(&tipc_nametbl_lock);
1036 str_len = tipc_printbuf_validate(&b);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001037
1038 skb_put(buf, TLV_SPACE(str_len));
1039 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
1040
1041 return buf;
1042}
1043
Per Liden4323add2006-01-18 00:38:21 +01001044int tipc_nametbl_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001045{
Allan Stephens4e3e6dc2008-05-12 15:41:53 -07001046 table.types = kcalloc(tipc_nametbl_size, sizeof(struct hlist_head),
1047 GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001048 if (!table.types)
1049 return -ENOMEM;
1050
Per Lidenb97bf3f2006-01-02 19:04:38 +01001051 table.local_publ_count = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001052 return 0;
1053}
1054
Per Liden4323add2006-01-18 00:38:21 +01001055void tipc_nametbl_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001056{
Per Lidenb97bf3f2006-01-02 19:04:38 +01001057 u32 i;
1058
1059 if (!table.types)
1060 return;
1061
Allan Stephensf1310722006-06-25 23:51:37 -07001062 /* Verify name table is empty, then release it */
1063
Per Liden4323add2006-01-18 00:38:21 +01001064 write_lock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001065 for (i = 0; i < tipc_nametbl_size; i++) {
Allan Stephensf1310722006-06-25 23:51:37 -07001066 if (!hlist_empty(&table.types[i]))
1067 err("tipc_nametbl_stop(): hash chain %u is non-null\n", i);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001068 }
1069 kfree(table.types);
1070 table.types = NULL;
Per Liden4323add2006-01-18 00:38:21 +01001071 write_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001072}
Allan Stephensf1310722006-06-25 23:51:37 -07001073