blob: 9cd58f8318f178b6132226c6612d90eef69e969b [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"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "name_table.h"
40#include "name_distr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010041#include "subscr.h"
42#include "port.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010043
Adrian Bunk988f0882006-03-20 22:37:52 -080044static int tipc_nametbl_size = 1024; /* must be a power of 2 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010045
46/**
Allan Stephensb52124a2011-05-30 09:44:38 -040047 * struct name_info - name sequence publication info
Allan Stephens968edbe2008-07-14 22:45:33 -070048 * @node_list: circular list of publications made by own node
49 * @cluster_list: circular list of publications made by own cluster
50 * @zone_list: circular list of publications made by own zone
51 * @node_list_size: number of entries in "node_list"
52 * @cluster_list_size: number of entries in "cluster_list"
53 * @zone_list_size: number of entries in "zone_list"
54 *
55 * Note: The zone list always contains at least one entry, since all
56 * publications of the associated name sequence belong to it.
57 * (The cluster and node lists may be empty.)
Per Lidenb97bf3f2006-01-02 19:04:38 +010058 */
59
Allan Stephensb52124a2011-05-30 09:44:38 -040060struct name_info {
Per Lidenb97bf3f2006-01-02 19:04:38 +010061 struct publication *node_list;
62 struct publication *cluster_list;
63 struct publication *zone_list;
Allan Stephens968edbe2008-07-14 22:45:33 -070064 u32 node_list_size;
65 u32 cluster_list_size;
66 u32 zone_list_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +010067};
68
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090069/**
Allan Stephensb52124a2011-05-30 09:44:38 -040070 * struct sub_seq - container for all published instances of a name sequence
71 * @lower: name sequence lower bound
72 * @upper: name sequence upper bound
73 * @info: pointer to name sequence publication info
74 */
75
76struct sub_seq {
77 u32 lower;
78 u32 upper;
79 struct name_info *info;
80};
81
82/**
Per Lidenb97bf3f2006-01-02 19:04:38 +010083 * struct name_seq - container for all published instances of a name type
84 * @type: 32 bit 'type' value for name sequence
85 * @sseq: pointer to dynamically-sized array of sub-sequences of this 'type';
86 * sub-sequences are sorted in ascending order
87 * @alloc: number of sub-sequences currently in array
Allan Stephensf1310722006-06-25 23:51:37 -070088 * @first_free: array index of first unused sub-sequence entry
Per Lidenb97bf3f2006-01-02 19:04:38 +010089 * @ns_list: links to adjacent name sequences in hash chain
90 * @subscriptions: list of subscriptions for this 'type'
Allan Stephens307fdf52008-06-04 17:38:22 -070091 * @lock: spinlock controlling access to publication lists of all sub-sequences
Per Lidenb97bf3f2006-01-02 19:04:38 +010092 */
93
94struct name_seq {
95 u32 type;
96 struct sub_seq *sseqs;
97 u32 alloc;
98 u32 first_free;
99 struct hlist_node ns_list;
100 struct list_head subscriptions;
101 spinlock_t lock;
102};
103
104/**
105 * struct name_table - table containing all existing port name publications
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900106 * @types: pointer to fixed-sized array of name sequence lists,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100107 * accessed via hashing on 'type'; name sequence lists are *not* sorted
108 * @local_publ_count: number of publications issued by this node
109 */
110
111struct name_table {
112 struct hlist_head *types;
113 u32 local_publ_count;
114};
115
Allan Stephense3ec9c72010-12-31 18:59:34 +0000116static struct name_table table;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100117static atomic_t rsv_publ_ok = ATOMIC_INIT(0);
Ingo Molnar34af9462006-06-27 02:53:55 -0700118DEFINE_RWLOCK(tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100119
120
Sam Ravnborg05790c62006-03-20 22:37:04 -0800121static int hash(int x)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100122{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000123 return x & (tipc_nametbl_size - 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100124}
125
126/**
127 * publ_create - create a publication structure
128 */
129
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900130static struct publication *publ_create(u32 type, u32 lower, u32 upper,
131 u32 scope, u32 node, u32 port_ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100132 u32 key)
133{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700134 struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100135 if (publ == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700136 warn("Publication creation failure, no memory\n");
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800137 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100138 }
139
Per Lidenb97bf3f2006-01-02 19:04:38 +0100140 publ->type = type;
141 publ->lower = lower;
142 publ->upper = upper;
143 publ->scope = scope;
144 publ->node = node;
145 publ->ref = port_ref;
146 publ->key = key;
147 INIT_LIST_HEAD(&publ->local_list);
148 INIT_LIST_HEAD(&publ->pport_list);
149 INIT_LIST_HEAD(&publ->subscr.nodesub_list);
150 return publ;
151}
152
153/**
Per Liden4323add2006-01-18 00:38:21 +0100154 * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
Per Lidenb97bf3f2006-01-02 19:04:38 +0100155 */
156
Adrian Bunk988f0882006-03-20 22:37:52 -0800157static struct sub_seq *tipc_subseq_alloc(u32 cnt)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100158{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700159 struct sub_seq *sseq = kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160 return sseq;
161}
162
163/**
Per Liden4323add2006-01-18 00:38:21 +0100164 * tipc_nameseq_create - create a name sequence structure for the specified 'type'
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900165 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166 * Allocates a single sub-sequence structure and sets it to all 0's.
167 */
168
Adrian Bunk988f0882006-03-20 22:37:52 -0800169static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100170{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700171 struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC);
Per Liden4323add2006-01-18 00:38:21 +0100172 struct sub_seq *sseq = tipc_subseq_alloc(1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173
174 if (!nseq || !sseq) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700175 warn("Name sequence creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100176 kfree(nseq);
177 kfree(sseq);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800178 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100179 }
180
Ingo Molnar34af9462006-06-27 02:53:55 -0700181 spin_lock_init(&nseq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100182 nseq->type = type;
183 nseq->sseqs = sseq;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100184 nseq->alloc = 1;
185 INIT_HLIST_NODE(&nseq->ns_list);
186 INIT_LIST_HEAD(&nseq->subscriptions);
187 hlist_add_head(&nseq->ns_list, seq_head);
188 return nseq;
189}
190
191/**
192 * nameseq_find_subseq - find sub-sequence (if any) matching a name instance
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900193 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100194 * Very time-critical, so binary searches through sub-sequence array.
195 */
196
Sam Ravnborg05790c62006-03-20 22:37:04 -0800197static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq,
198 u32 instance)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100199{
200 struct sub_seq *sseqs = nseq->sseqs;
201 int low = 0;
202 int high = nseq->first_free - 1;
203 int mid;
204
205 while (low <= high) {
206 mid = (low + high) / 2;
207 if (instance < sseqs[mid].lower)
208 high = mid - 1;
209 else if (instance > sseqs[mid].upper)
210 low = mid + 1;
211 else
212 return &sseqs[mid];
213 }
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800214 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100215}
216
217/**
218 * nameseq_locate_subseq - determine position of name instance in sub-sequence
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900219 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220 * Returns index in sub-sequence array of the entry that contains the specified
221 * instance value; if no entry contains that value, returns the position
222 * where a new entry for it would be inserted in the array.
223 *
224 * Note: Similar to binary search code for locating a sub-sequence.
225 */
226
227static u32 nameseq_locate_subseq(struct name_seq *nseq, u32 instance)
228{
229 struct sub_seq *sseqs = nseq->sseqs;
230 int low = 0;
231 int high = nseq->first_free - 1;
232 int mid;
233
234 while (low <= high) {
235 mid = (low + high) / 2;
236 if (instance < sseqs[mid].lower)
237 high = mid - 1;
238 else if (instance > sseqs[mid].upper)
239 low = mid + 1;
240 else
241 return mid;
242 }
243 return low;
244}
245
246/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900247 * tipc_nameseq_insert_publ -
Per Lidenb97bf3f2006-01-02 19:04:38 +0100248 */
249
Adrian Bunk988f0882006-03-20 22:37:52 -0800250static struct publication *tipc_nameseq_insert_publ(struct name_seq *nseq,
251 u32 type, u32 lower, u32 upper,
252 u32 scope, u32 node, u32 port, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100253{
254 struct subscription *s;
255 struct subscription *st;
256 struct publication *publ;
257 struct sub_seq *sseq;
Allan Stephensb52124a2011-05-30 09:44:38 -0400258 struct name_info *info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100259 int created_subseq = 0;
260
Per Lidenb97bf3f2006-01-02 19:04:38 +0100261 sseq = nameseq_find_subseq(nseq, lower);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100262 if (sseq) {
263
264 /* Lower end overlaps existing entry => need an exact match */
265
266 if ((sseq->lower != lower) || (sseq->upper != upper)) {
Allan Stephensf1310722006-06-25 23:51:37 -0700267 warn("Cannot publish {%u,%u,%u}, overlap error\n",
268 type, lower, upper);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800269 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100270 }
Allan Stephensb52124a2011-05-30 09:44:38 -0400271
272 info = sseq->info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100273 } else {
274 u32 inspos;
275 struct sub_seq *freesseq;
276
277 /* Find where lower end should be inserted */
278
279 inspos = nameseq_locate_subseq(nseq, lower);
280
281 /* Fail if upper end overlaps into an existing entry */
282
283 if ((inspos < nseq->first_free) &&
284 (upper >= nseq->sseqs[inspos].lower)) {
Allan Stephensf1310722006-06-25 23:51:37 -0700285 warn("Cannot publish {%u,%u,%u}, overlap error\n",
286 type, lower, upper);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800287 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100288 }
289
290 /* Ensure there is space for new sub-sequence */
291
292 if (nseq->first_free == nseq->alloc) {
Allan Stephens9ab230f2006-06-25 23:37:24 -0700293 struct sub_seq *sseqs = tipc_subseq_alloc(nseq->alloc * 2);
294
295 if (!sseqs) {
Allan Stephensf1310722006-06-25 23:51:37 -0700296 warn("Cannot publish {%u,%u,%u}, no memory\n",
297 type, lower, upper);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800298 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100299 }
Allan Stephens9ab230f2006-06-25 23:37:24 -0700300 memcpy(sseqs, nseq->sseqs,
301 nseq->alloc * sizeof(struct sub_seq));
302 kfree(nseq->sseqs);
303 nseq->sseqs = sseqs;
304 nseq->alloc *= 2;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100305 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100306
Allan Stephensb52124a2011-05-30 09:44:38 -0400307 info = kzalloc(sizeof(*info), GFP_ATOMIC);
308 if (!info) {
309 warn("Cannot publish {%u,%u,%u}, no memory\n",
310 type, lower, upper);
311 return NULL;
312 }
313
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314 /* Insert new sub-sequence */
315
Per Lidenb97bf3f2006-01-02 19:04:38 +0100316 sseq = &nseq->sseqs[inspos];
317 freesseq = &nseq->sseqs[nseq->first_free];
Allan Stephens0e659672010-12-31 18:59:32 +0000318 memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof(*sseq));
319 memset(sseq, 0, sizeof(*sseq));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320 nseq->first_free++;
321 sseq->lower = lower;
322 sseq->upper = upper;
Allan Stephensb52124a2011-05-30 09:44:38 -0400323 sseq->info = info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324 created_subseq = 1;
325 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100326
327 /* Insert a publication: */
328
329 publ = publ_create(type, lower, upper, scope, node, port, key);
330 if (!publ)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800331 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332
Allan Stephensb52124a2011-05-30 09:44:38 -0400333 info->zone_list_size++;
334 if (!info->zone_list)
335 info->zone_list = publ->zone_list_next = publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100336 else {
Allan Stephensb52124a2011-05-30 09:44:38 -0400337 publ->zone_list_next = info->zone_list->zone_list_next;
338 info->zone_list->zone_list_next = publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100339 }
340
341 if (in_own_cluster(node)) {
Allan Stephensb52124a2011-05-30 09:44:38 -0400342 info->cluster_list_size++;
343 if (!info->cluster_list)
344 info->cluster_list = publ->cluster_list_next = publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345 else {
346 publ->cluster_list_next =
Allan Stephensb52124a2011-05-30 09:44:38 -0400347 info->cluster_list->cluster_list_next;
348 info->cluster_list->cluster_list_next = publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349 }
350 }
351
352 if (node == tipc_own_addr) {
Allan Stephensb52124a2011-05-30 09:44:38 -0400353 info->node_list_size++;
354 if (!info->node_list)
355 info->node_list = publ->node_list_next = publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100356 else {
Allan Stephensb52124a2011-05-30 09:44:38 -0400357 publ->node_list_next = info->node_list->node_list_next;
358 info->node_list->node_list_next = publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359 }
360 }
361
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900362 /*
363 * Any subscriptions waiting for notification?
Per Lidenb97bf3f2006-01-02 19:04:38 +0100364 */
365 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
Per Liden4323add2006-01-18 00:38:21 +0100366 tipc_subscr_report_overlap(s,
367 publ->lower,
368 publ->upper,
369 TIPC_PUBLISHED,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900370 publ->ref,
Per Liden4323add2006-01-18 00:38:21 +0100371 publ->node,
372 created_subseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100373 }
374 return publ;
375}
376
377/**
Per Liden4323add2006-01-18 00:38:21 +0100378 * tipc_nameseq_remove_publ -
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900379 *
Allan Stephensf1310722006-06-25 23:51:37 -0700380 * NOTE: There may be cases where TIPC is asked to remove a publication
381 * that is not in the name table. For example, if another node issues a
382 * publication for a name sequence that overlaps an existing name sequence
383 * the publication will not be recorded, which means the publication won't
384 * be found when the name sequence is later withdrawn by that node.
385 * A failed withdraw request simply returns a failure indication and lets the
386 * caller issue any error or warning messages associated with such a problem.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387 */
388
Adrian Bunk988f0882006-03-20 22:37:52 -0800389static struct publication *tipc_nameseq_remove_publ(struct name_seq *nseq, u32 inst,
390 u32 node, u32 ref, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391{
392 struct publication *publ;
Allan Stephensf1310722006-06-25 23:51:37 -0700393 struct publication *curr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100394 struct publication *prev;
395 struct sub_seq *sseq = nameseq_find_subseq(nseq, inst);
Allan Stephensb52124a2011-05-30 09:44:38 -0400396 struct name_info *info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100397 struct sub_seq *free;
398 struct subscription *s, *st;
399 int removed_subseq = 0;
400
Allan Stephensf1310722006-06-25 23:51:37 -0700401 if (!sseq)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800402 return NULL;
Allan Stephensf1310722006-06-25 23:51:37 -0700403
Allan Stephensb52124a2011-05-30 09:44:38 -0400404 info = sseq->info;
405
Allan Stephensf1310722006-06-25 23:51:37 -0700406 /* Remove publication from zone scope list */
407
Allan Stephensb52124a2011-05-30 09:44:38 -0400408 prev = info->zone_list;
409 publ = info->zone_list->zone_list_next;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900410 while ((publ->key != key) || (publ->ref != ref) ||
Per Lidenb97bf3f2006-01-02 19:04:38 +0100411 (publ->node && (publ->node != node))) {
412 prev = publ;
413 publ = publ->zone_list_next;
Allan Stephensb52124a2011-05-30 09:44:38 -0400414 if (prev == info->zone_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900415
Allan Stephensf1310722006-06-25 23:51:37 -0700416 /* Prevent endless loop if publication not found */
417
418 return NULL;
419 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100420 }
Allan Stephensb52124a2011-05-30 09:44:38 -0400421 if (publ != info->zone_list)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422 prev->zone_list_next = publ->zone_list_next;
423 else if (publ->zone_list_next != publ) {
424 prev->zone_list_next = publ->zone_list_next;
Allan Stephensb52124a2011-05-30 09:44:38 -0400425 info->zone_list = publ->zone_list_next;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426 } else {
Allan Stephensb52124a2011-05-30 09:44:38 -0400427 info->zone_list = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100428 }
Allan Stephensb52124a2011-05-30 09:44:38 -0400429 info->zone_list_size--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430
Allan Stephensf1310722006-06-25 23:51:37 -0700431 /* Remove publication from cluster scope list, if present */
432
Per Lidenb97bf3f2006-01-02 19:04:38 +0100433 if (in_own_cluster(node)) {
Allan Stephensb52124a2011-05-30 09:44:38 -0400434 prev = info->cluster_list;
435 curr = info->cluster_list->cluster_list_next;
Allan Stephensf1310722006-06-25 23:51:37 -0700436 while (curr != publ) {
437 prev = curr;
438 curr = curr->cluster_list_next;
Allan Stephensb52124a2011-05-30 09:44:38 -0400439 if (prev == info->cluster_list) {
Allan Stephensf1310722006-06-25 23:51:37 -0700440
441 /* Prevent endless loop for malformed list */
442
443 err("Unable to de-list cluster publication\n"
444 "{%u%u}, node=0x%x, ref=%u, key=%u)\n",
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900445 publ->type, publ->lower, publ->node,
Allan Stephensf1310722006-06-25 23:51:37 -0700446 publ->ref, publ->key);
447 goto end_cluster;
448 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100449 }
Allan Stephensb52124a2011-05-30 09:44:38 -0400450 if (publ != info->cluster_list)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100451 prev->cluster_list_next = publ->cluster_list_next;
452 else if (publ->cluster_list_next != publ) {
453 prev->cluster_list_next = publ->cluster_list_next;
Allan Stephensb52124a2011-05-30 09:44:38 -0400454 info->cluster_list = publ->cluster_list_next;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100455 } else {
Allan Stephensb52124a2011-05-30 09:44:38 -0400456 info->cluster_list = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457 }
Allan Stephensb52124a2011-05-30 09:44:38 -0400458 info->cluster_list_size--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459 }
Allan Stephensf1310722006-06-25 23:51:37 -0700460end_cluster:
461
462 /* Remove publication from node scope list, if present */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100463
464 if (node == tipc_own_addr) {
Allan Stephensb52124a2011-05-30 09:44:38 -0400465 prev = info->node_list;
466 curr = info->node_list->node_list_next;
Allan Stephensf1310722006-06-25 23:51:37 -0700467 while (curr != publ) {
468 prev = curr;
469 curr = curr->node_list_next;
Allan Stephensb52124a2011-05-30 09:44:38 -0400470 if (prev == info->node_list) {
Allan Stephensf1310722006-06-25 23:51:37 -0700471
472 /* Prevent endless loop for malformed list */
473
474 err("Unable to de-list node publication\n"
475 "{%u%u}, node=0x%x, ref=%u, key=%u)\n",
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900476 publ->type, publ->lower, publ->node,
Allan Stephensf1310722006-06-25 23:51:37 -0700477 publ->ref, publ->key);
478 goto end_node;
479 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100480 }
Allan Stephensb52124a2011-05-30 09:44:38 -0400481 if (publ != info->node_list)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100482 prev->node_list_next = publ->node_list_next;
483 else if (publ->node_list_next != publ) {
484 prev->node_list_next = publ->node_list_next;
Allan Stephensb52124a2011-05-30 09:44:38 -0400485 info->node_list = publ->node_list_next;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100486 } else {
Allan Stephensb52124a2011-05-30 09:44:38 -0400487 info->node_list = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488 }
Allan Stephensb52124a2011-05-30 09:44:38 -0400489 info->node_list_size--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100490 }
Allan Stephensf1310722006-06-25 23:51:37 -0700491end_node:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100492
Allan Stephensf1310722006-06-25 23:51:37 -0700493 /* Contract subseq list if no more publications for that subseq */
494
Allan Stephensb52124a2011-05-30 09:44:38 -0400495 if (!info->zone_list) {
496 kfree(info);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497 free = &nseq->sseqs[nseq->first_free--];
Allan Stephens0e659672010-12-31 18:59:32 +0000498 memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499 removed_subseq = 1;
500 }
501
Allan Stephensf1310722006-06-25 23:51:37 -0700502 /* Notify any waiting subscriptions */
503
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
Per Liden4323add2006-01-18 00:38:21 +0100505 tipc_subscr_report_overlap(s,
506 publ->lower,
507 publ->upper,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900508 TIPC_WITHDRAWN,
509 publ->ref,
Per Liden4323add2006-01-18 00:38:21 +0100510 publ->node,
511 removed_subseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100512 }
Allan Stephensf1310722006-06-25 23:51:37 -0700513
Per Lidenb97bf3f2006-01-02 19:04:38 +0100514 return publ;
515}
516
517/**
Per Liden4323add2006-01-18 00:38:21 +0100518 * tipc_nameseq_subscribe: attach a subscription, and issue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100519 * the prescribed number of events if there is any sub-
520 * sequence overlapping with the requested sequence
521 */
522
Florian Westphal248bbf32007-08-02 19:26:23 -0700523static void tipc_nameseq_subscribe(struct name_seq *nseq, struct subscription *s)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100524{
525 struct sub_seq *sseq = nseq->sseqs;
526
527 list_add(&s->nameseq_list, &nseq->subscriptions);
528
529 if (!sseq)
530 return;
531
532 while (sseq != &nseq->sseqs[nseq->first_free]) {
Allan Stephensb52124a2011-05-30 09:44:38 -0400533 struct publication *zl = sseq->info->zone_list;
Allan Stephens0e659672010-12-31 18:59:32 +0000534 if (zl && tipc_subscr_overlap(s, sseq->lower, sseq->upper)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535 struct publication *crs = zl;
536 int must_report = 1;
537
538 do {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900539 tipc_subscr_report_overlap(s,
540 sseq->lower,
Per Liden4323add2006-01-18 00:38:21 +0100541 sseq->upper,
542 TIPC_PUBLISHED,
543 crs->ref,
544 crs->node,
545 must_report);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100546 must_report = 0;
547 crs = crs->zone_list_next;
548 } while (crs != zl);
549 }
550 sseq++;
551 }
552}
553
554static struct name_seq *nametbl_find_seq(u32 type)
555{
556 struct hlist_head *seq_head;
557 struct hlist_node *seq_node;
558 struct name_seq *ns;
559
Per Lidenb97bf3f2006-01-02 19:04:38 +0100560 seq_head = &table.types[hash(type)];
561 hlist_for_each_entry(ns, seq_node, seq_head, ns_list) {
Allan Stephensb29f1422010-12-31 18:59:25 +0000562 if (ns->type == type)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100563 return ns;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100564 }
565
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800566 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100567};
568
Per Liden4323add2006-01-18 00:38:21 +0100569struct publication *tipc_nametbl_insert_publ(u32 type, u32 lower, u32 upper,
570 u32 scope, u32 node, u32 port, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100571{
572 struct name_seq *seq = nametbl_find_seq(type);
573
Per Lidenb97bf3f2006-01-02 19:04:38 +0100574 if (lower > upper) {
Allan Stephensf1310722006-06-25 23:51:37 -0700575 warn("Failed to publish illegal {%u,%u,%u}\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100576 type, lower, upper);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800577 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578 }
579
Allan Stephensb29f1422010-12-31 18:59:25 +0000580 if (!seq)
Per Liden4323add2006-01-18 00:38:21 +0100581 seq = tipc_nameseq_create(type, &table.types[hash(type)]);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 if (!seq)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800583 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100584
Per Liden4323add2006-01-18 00:38:21 +0100585 return tipc_nameseq_insert_publ(seq, type, lower, upper,
586 scope, node, port, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100587}
588
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900589struct publication *tipc_nametbl_remove_publ(u32 type, u32 lower,
Per Liden4323add2006-01-18 00:38:21 +0100590 u32 node, u32 ref, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591{
592 struct publication *publ;
593 struct name_seq *seq = nametbl_find_seq(type);
594
595 if (!seq)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800596 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100597
Per Liden4323add2006-01-18 00:38:21 +0100598 publ = tipc_nameseq_remove_publ(seq, lower, node, ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100599
600 if (!seq->first_free && list_empty(&seq->subscriptions)) {
601 hlist_del_init(&seq->ns_list);
602 kfree(seq->sseqs);
603 kfree(seq);
604 }
605 return publ;
606}
607
608/*
Allan Stephens5d9c54c2010-09-03 08:33:39 +0000609 * tipc_nametbl_translate - translate name to port id
Per Lidenb97bf3f2006-01-02 19:04:38 +0100610 *
611 * Note: on entry 'destnode' is the search domain used during translation;
612 * on exit it passes back the node address of the matching port (if any)
613 */
614
Per Liden4323add2006-01-18 00:38:21 +0100615u32 tipc_nametbl_translate(u32 type, u32 instance, u32 *destnode)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100616{
617 struct sub_seq *sseq;
Allan Stephensb52124a2011-05-30 09:44:38 -0400618 struct name_info *info;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800619 struct publication *publ = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620 struct name_seq *seq;
621 u32 ref;
622
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000623 if (!tipc_in_scope(*destnode, tipc_own_addr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624 return 0;
625
Per Liden4323add2006-01-18 00:38:21 +0100626 read_lock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100627 seq = nametbl_find_seq(type);
628 if (unlikely(!seq))
629 goto not_found;
630 sseq = nameseq_find_subseq(seq, instance);
631 if (unlikely(!sseq))
632 goto not_found;
633 spin_lock_bh(&seq->lock);
Allan Stephensb52124a2011-05-30 09:44:38 -0400634 info = sseq->info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100635
636 /* Closest-First Algorithm: */
637 if (likely(!*destnode)) {
Allan Stephensb52124a2011-05-30 09:44:38 -0400638 publ = info->node_list;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100639 if (publ) {
Allan Stephensb52124a2011-05-30 09:44:38 -0400640 info->node_list = publ->node_list_next;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641found:
642 ref = publ->ref;
643 *destnode = publ->node;
644 spin_unlock_bh(&seq->lock);
Per Liden4323add2006-01-18 00:38:21 +0100645 read_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646 return ref;
647 }
Allan Stephensb52124a2011-05-30 09:44:38 -0400648 publ = info->cluster_list;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100649 if (publ) {
Allan Stephensb52124a2011-05-30 09:44:38 -0400650 info->cluster_list = publ->cluster_list_next;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651 goto found;
652 }
Allan Stephensb52124a2011-05-30 09:44:38 -0400653 publ = info->zone_list;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654 if (publ) {
Allan Stephensb52124a2011-05-30 09:44:38 -0400655 info->zone_list = publ->zone_list_next;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100656 goto found;
657 }
658 }
659
660 /* Round-Robin Algorithm: */
661 else if (*destnode == tipc_own_addr) {
Allan Stephensb52124a2011-05-30 09:44:38 -0400662 publ = info->node_list;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100663 if (publ) {
Allan Stephensb52124a2011-05-30 09:44:38 -0400664 info->node_list = publ->node_list_next;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100665 goto found;
666 }
667 } else if (in_own_cluster(*destnode)) {
Allan Stephensb52124a2011-05-30 09:44:38 -0400668 publ = info->cluster_list;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669 if (publ) {
Allan Stephensb52124a2011-05-30 09:44:38 -0400670 info->cluster_list = publ->cluster_list_next;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100671 goto found;
672 }
673 } else {
Allan Stephensb52124a2011-05-30 09:44:38 -0400674 publ = info->zone_list;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100675 if (publ) {
Allan Stephensb52124a2011-05-30 09:44:38 -0400676 info->zone_list = publ->zone_list_next;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100677 goto found;
678 }
679 }
680 spin_unlock_bh(&seq->lock);
681not_found:
Per Liden4323add2006-01-18 00:38:21 +0100682 read_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100683 return 0;
684}
685
686/**
Per Liden4323add2006-01-18 00:38:21 +0100687 * tipc_nametbl_mc_translate - find multicast destinations
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900688 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100689 * Creates list of all local ports that overlap the given multicast address;
690 * also determines if any off-node ports overlap.
691 *
692 * Note: Publications with a scope narrower than 'limit' are ignored.
693 * (i.e. local node-scope publications mustn't receive messages arriving
694 * from another node, even if the multcast link brought it here)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900695 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100696 * Returns non-zero if any off-node ports overlap
697 */
698
Per Liden4323add2006-01-18 00:38:21 +0100699int tipc_nametbl_mc_translate(u32 type, u32 lower, u32 upper, u32 limit,
700 struct port_list *dports)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100701{
702 struct name_seq *seq;
703 struct sub_seq *sseq;
704 struct sub_seq *sseq_stop;
Allan Stephensb52124a2011-05-30 09:44:38 -0400705 struct name_info *info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100706 int res = 0;
707
Per Liden4323add2006-01-18 00:38:21 +0100708 read_lock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100709 seq = nametbl_find_seq(type);
710 if (!seq)
711 goto exit;
712
713 spin_lock_bh(&seq->lock);
714
715 sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
716 sseq_stop = seq->sseqs + seq->first_free;
717 for (; sseq != sseq_stop; sseq++) {
718 struct publication *publ;
719
720 if (sseq->lower > upper)
721 break;
Allan Stephens968edbe2008-07-14 22:45:33 -0700722
Allan Stephensb52124a2011-05-30 09:44:38 -0400723 info = sseq->info;
724 publ = info->node_list;
Allan Stephens968edbe2008-07-14 22:45:33 -0700725 if (publ) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100726 do {
Allan Stephens968edbe2008-07-14 22:45:33 -0700727 if (publ->scope <= limit)
Per Liden4323add2006-01-18 00:38:21 +0100728 tipc_port_list_add(dports, publ->ref);
Allan Stephens968edbe2008-07-14 22:45:33 -0700729 publ = publ->node_list_next;
Allan Stephensb52124a2011-05-30 09:44:38 -0400730 } while (publ != info->node_list);
Allan Stephens968edbe2008-07-14 22:45:33 -0700731 }
732
Allan Stephensb52124a2011-05-30 09:44:38 -0400733 if (info->cluster_list_size != info->node_list_size)
Allan Stephens968edbe2008-07-14 22:45:33 -0700734 res = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100735 }
736
737 spin_unlock_bh(&seq->lock);
738exit:
Per Liden4323add2006-01-18 00:38:21 +0100739 read_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100740 return res;
741}
742
743/**
Per Liden4323add2006-01-18 00:38:21 +0100744 * tipc_nametbl_publish_rsv - publish port name using a reserved name type
Per Lidenb97bf3f2006-01-02 19:04:38 +0100745 */
746
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900747int tipc_nametbl_publish_rsv(u32 ref, unsigned int scope,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100748 struct tipc_name_seq const *seq)
749{
750 int res;
751
752 atomic_inc(&rsv_publ_ok);
753 res = tipc_publish(ref, scope, seq);
754 atomic_dec(&rsv_publ_ok);
755 return res;
756}
757
758/**
Per Liden4323add2006-01-18 00:38:21 +0100759 * tipc_nametbl_publish - add name publication to network name tables
Per Lidenb97bf3f2006-01-02 19:04:38 +0100760 */
761
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900762struct publication *tipc_nametbl_publish(u32 type, u32 lower, u32 upper,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100763 u32 scope, u32 port_ref, u32 key)
764{
765 struct publication *publ;
766
767 if (table.local_publ_count >= tipc_max_publications) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900768 warn("Publication failed, local publication limit reached (%u)\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100769 tipc_max_publications);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800770 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100771 }
772 if ((type < TIPC_RESERVED_TYPES) && !atomic_read(&rsv_publ_ok)) {
Allan Stephensf1310722006-06-25 23:51:37 -0700773 warn("Publication failed, reserved name {%u,%u,%u}\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100774 type, lower, upper);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800775 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100776 }
777
Per Liden4323add2006-01-18 00:38:21 +0100778 write_lock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100779 table.local_publ_count++;
Per Liden4323add2006-01-18 00:38:21 +0100780 publ = tipc_nametbl_insert_publ(type, lower, upper, scope,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100781 tipc_own_addr, port_ref, key);
Allan Stephensa0168922010-12-31 18:59:35 +0000782 if (publ && (scope != TIPC_NODE_SCOPE))
Per Liden4323add2006-01-18 00:38:21 +0100783 tipc_named_publish(publ);
Per Liden4323add2006-01-18 00:38:21 +0100784 write_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100785 return publ;
786}
787
788/**
Per Liden4323add2006-01-18 00:38:21 +0100789 * tipc_nametbl_withdraw - withdraw name publication from network name tables
Per Lidenb97bf3f2006-01-02 19:04:38 +0100790 */
791
Per Liden4323add2006-01-18 00:38:21 +0100792int tipc_nametbl_withdraw(u32 type, u32 lower, u32 ref, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100793{
794 struct publication *publ;
795
Per Liden4323add2006-01-18 00:38:21 +0100796 write_lock_bh(&tipc_nametbl_lock);
797 publ = tipc_nametbl_remove_publ(type, lower, tipc_own_addr, ref, key);
Allan Stephensf1310722006-06-25 23:51:37 -0700798 if (likely(publ)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100799 table.local_publ_count--;
800 if (publ->scope != TIPC_NODE_SCOPE)
Per Liden4323add2006-01-18 00:38:21 +0100801 tipc_named_withdraw(publ);
802 write_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100803 list_del_init(&publ->pport_list);
804 kfree(publ);
805 return 1;
806 }
Per Liden4323add2006-01-18 00:38:21 +0100807 write_unlock_bh(&tipc_nametbl_lock);
Allan Stephensf1310722006-06-25 23:51:37 -0700808 err("Unable to remove local publication\n"
809 "(type=%u, lower=%u, ref=%u, key=%u)\n",
810 type, lower, ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100811 return 0;
812}
813
814/**
Per Liden4323add2006-01-18 00:38:21 +0100815 * tipc_nametbl_subscribe - add a subscription object to the name table
Per Lidenb97bf3f2006-01-02 19:04:38 +0100816 */
817
Allan Stephensf1310722006-06-25 23:51:37 -0700818void tipc_nametbl_subscribe(struct subscription *s)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100819{
820 u32 type = s->seq.type;
821 struct name_seq *seq;
822
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900823 write_lock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100824 seq = nametbl_find_seq(type);
Allan Stephensa0168922010-12-31 18:59:35 +0000825 if (!seq)
Per Liden4323add2006-01-18 00:38:21 +0100826 seq = tipc_nameseq_create(type, &table.types[hash(type)]);
Allan Stephens0e659672010-12-31 18:59:32 +0000827 if (seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900828 spin_lock_bh(&seq->lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900829 tipc_nameseq_subscribe(seq, s);
830 spin_unlock_bh(&seq->lock);
831 } else {
Allan Stephensf1310722006-06-25 23:51:37 -0700832 warn("Failed to create subscription for {%u,%u,%u}\n",
833 s->seq.type, s->seq.lower, s->seq.upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900834 }
835 write_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100836}
837
838/**
Per Liden4323add2006-01-18 00:38:21 +0100839 * tipc_nametbl_unsubscribe - remove a subscription object from name table
Per Lidenb97bf3f2006-01-02 19:04:38 +0100840 */
841
Allan Stephensf1310722006-06-25 23:51:37 -0700842void tipc_nametbl_unsubscribe(struct subscription *s)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100843{
844 struct name_seq *seq;
845
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900846 write_lock_bh(&tipc_nametbl_lock);
847 seq = nametbl_find_seq(s->seq.type);
Allan Stephens0e659672010-12-31 18:59:32 +0000848 if (seq != NULL) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900849 spin_lock_bh(&seq->lock);
850 list_del_init(&s->nameseq_list);
851 spin_unlock_bh(&seq->lock);
852 if ((seq->first_free == 0) && list_empty(&seq->subscriptions)) {
853 hlist_del_init(&seq->ns_list);
854 kfree(seq->sseqs);
855 kfree(seq);
856 }
857 }
858 write_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100859}
860
861
862/**
863 * subseq_list: print specified sub-sequence contents into the given buffer
864 */
865
866static void subseq_list(struct sub_seq *sseq, struct print_buf *buf, u32 depth,
867 u32 index)
868{
869 char portIdStr[27];
Allan Stephensc2de5812010-08-17 11:00:14 +0000870 const char *scope_str[] = {"", " zone", " cluster", " node"};
Allan Stephensb52124a2011-05-30 09:44:38 -0400871 struct publication *publ = sseq->info->zone_list;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100872
873 tipc_printf(buf, "%-10u %-10u ", sseq->lower, sseq->upper);
874
875 if (depth == 2 || !publ) {
876 tipc_printf(buf, "\n");
877 return;
878 }
879
880 do {
Allan Stephens0e659672010-12-31 18:59:32 +0000881 sprintf(portIdStr, "<%u.%u.%u:%u>",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100882 tipc_zone(publ->node), tipc_cluster(publ->node),
883 tipc_node(publ->node), publ->ref);
884 tipc_printf(buf, "%-26s ", portIdStr);
885 if (depth > 3) {
Allan Stephensc2de5812010-08-17 11:00:14 +0000886 tipc_printf(buf, "%-10u %s", publ->key,
887 scope_str[publ->scope]);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100888 }
889
890 publ = publ->zone_list_next;
Allan Stephensb52124a2011-05-30 09:44:38 -0400891 if (publ == sseq->info->zone_list)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100892 break;
893
894 tipc_printf(buf, "\n%33s", " ");
895 } while (1);
896
897 tipc_printf(buf, "\n");
898}
899
900/**
901 * nameseq_list: print specified name sequence contents into the given buffer
902 */
903
904static void nameseq_list(struct name_seq *seq, struct print_buf *buf, u32 depth,
905 u32 type, u32 lowbound, u32 upbound, u32 index)
906{
907 struct sub_seq *sseq;
908 char typearea[11];
909
Allan Stephens0f15d362008-06-04 17:37:59 -0700910 if (seq->first_free == 0)
911 return;
912
Per Lidenb97bf3f2006-01-02 19:04:38 +0100913 sprintf(typearea, "%-10u", seq->type);
914
915 if (depth == 1) {
916 tipc_printf(buf, "%s\n", typearea);
917 return;
918 }
919
920 for (sseq = seq->sseqs; sseq != &seq->sseqs[seq->first_free]; sseq++) {
921 if ((lowbound <= sseq->upper) && (upbound >= sseq->lower)) {
922 tipc_printf(buf, "%s ", typearea);
Allan Stephens307fdf52008-06-04 17:38:22 -0700923 spin_lock_bh(&seq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100924 subseq_list(sseq, buf, depth, index);
Allan Stephens307fdf52008-06-04 17:38:22 -0700925 spin_unlock_bh(&seq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100926 sprintf(typearea, "%10s", " ");
927 }
928 }
929}
930
931/**
932 * nametbl_header - print name table header into the given buffer
933 */
934
935static void nametbl_header(struct print_buf *buf, u32 depth)
936{
Allan Stephensc2de5812010-08-17 11:00:14 +0000937 const char *header[] = {
938 "Type ",
939 "Lower Upper ",
940 "Port Identity ",
941 "Publication Scope"
942 };
Per Lidenb97bf3f2006-01-02 19:04:38 +0100943
Allan Stephensc2de5812010-08-17 11:00:14 +0000944 int i;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100945
Allan Stephensc2de5812010-08-17 11:00:14 +0000946 if (depth > 4)
947 depth = 4;
948 for (i = 0; i < depth; i++)
949 tipc_printf(buf, header[i]);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100950 tipc_printf(buf, "\n");
951}
952
953/**
954 * nametbl_list - print specified name table contents into the given buffer
955 */
956
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900957static void nametbl_list(struct print_buf *buf, u32 depth_info,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100958 u32 type, u32 lowbound, u32 upbound)
959{
960 struct hlist_head *seq_head;
961 struct hlist_node *seq_node;
962 struct name_seq *seq;
963 int all_types;
964 u32 depth;
965 u32 i;
966
967 all_types = (depth_info & TIPC_NTQ_ALLTYPES);
968 depth = (depth_info & ~TIPC_NTQ_ALLTYPES);
969
970 if (depth == 0)
971 return;
972
973 if (all_types) {
974 /* display all entries in name table to specified depth */
975 nametbl_header(buf, depth);
976 lowbound = 0;
977 upbound = ~0;
978 for (i = 0; i < tipc_nametbl_size; i++) {
979 seq_head = &table.types[i];
980 hlist_for_each_entry(seq, seq_node, seq_head, ns_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900981 nameseq_list(seq, buf, depth, seq->type,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100982 lowbound, upbound, i);
983 }
984 }
985 } else {
986 /* display only the sequence that matches the specified type */
987 if (upbound < lowbound) {
988 tipc_printf(buf, "invalid name sequence specified\n");
989 return;
990 }
991 nametbl_header(buf, depth);
992 i = hash(type);
993 seq_head = &table.types[i];
994 hlist_for_each_entry(seq, seq_node, seq_head, ns_list) {
995 if (seq->type == type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900996 nameseq_list(seq, buf, depth, type,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100997 lowbound, upbound, i);
998 break;
999 }
1000 }
1001 }
1002}
1003
Per Lidenb97bf3f2006-01-02 19:04:38 +01001004#define MAX_NAME_TBL_QUERY 32768
1005
Per Liden4323add2006-01-18 00:38:21 +01001006struct sk_buff *tipc_nametbl_get(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001007{
1008 struct sk_buff *buf;
1009 struct tipc_name_table_query *argv;
1010 struct tlv_desc *rep_tlv;
1011 struct print_buf b;
1012 int str_len;
1013
1014 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NAME_TBL_QUERY))
Per Liden4323add2006-01-18 00:38:21 +01001015 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001016
Per Liden4323add2006-01-18 00:38:21 +01001017 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_NAME_TBL_QUERY));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001018 if (!buf)
1019 return NULL;
1020
1021 rep_tlv = (struct tlv_desc *)buf->data;
Per Liden4323add2006-01-18 00:38:21 +01001022 tipc_printbuf_init(&b, TLV_DATA(rep_tlv), MAX_NAME_TBL_QUERY);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001023 argv = (struct tipc_name_table_query *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +01001024 read_lock_bh(&tipc_nametbl_lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001025 nametbl_list(&b, ntohl(argv->depth), ntohl(argv->type),
Per Lidenb97bf3f2006-01-02 19:04:38 +01001026 ntohl(argv->lowbound), ntohl(argv->upbound));
Per Liden4323add2006-01-18 00:38:21 +01001027 read_unlock_bh(&tipc_nametbl_lock);
1028 str_len = tipc_printbuf_validate(&b);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001029
1030 skb_put(buf, TLV_SPACE(str_len));
1031 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
1032
1033 return buf;
1034}
1035
Per Liden4323add2006-01-18 00:38:21 +01001036int tipc_nametbl_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001037{
Allan Stephens4e3e6dc2008-05-12 15:41:53 -07001038 table.types = kcalloc(tipc_nametbl_size, sizeof(struct hlist_head),
1039 GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001040 if (!table.types)
1041 return -ENOMEM;
1042
Per Lidenb97bf3f2006-01-02 19:04:38 +01001043 table.local_publ_count = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001044 return 0;
1045}
1046
Per Liden4323add2006-01-18 00:38:21 +01001047void tipc_nametbl_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001048{
Per Lidenb97bf3f2006-01-02 19:04:38 +01001049 u32 i;
1050
1051 if (!table.types)
1052 return;
1053
Allan Stephensf1310722006-06-25 23:51:37 -07001054 /* Verify name table is empty, then release it */
1055
Per Liden4323add2006-01-18 00:38:21 +01001056 write_lock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001057 for (i = 0; i < tipc_nametbl_size; i++) {
Allan Stephensf1310722006-06-25 23:51:37 -07001058 if (!hlist_empty(&table.types[i]))
1059 err("tipc_nametbl_stop(): hash chain %u is non-null\n", i);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001060 }
1061 kfree(table.types);
1062 table.types = NULL;
Per Liden4323add2006-01-18 00:38:21 +01001063 write_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001064}
Allan Stephensf1310722006-06-25 23:51:37 -07001065