blob: 042e8e3cabc09f84aa5dce626c57a30faf3ca32d [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 Stephensf6f0a4d2011-05-30 10:48:48 -04005 * Copyright (c) 2004-2008, 2010-2011, 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
Ying Xuef046e7d2012-08-16 12:09:11 +000044#define 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 */
Allan Stephensb52124a2011-05-30 09:44:38 -040059struct name_info {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -040060 struct list_head node_list;
61 struct list_head cluster_list;
62 struct list_head zone_list;
Allan Stephens968edbe2008-07-14 22:45:33 -070063 u32 node_list_size;
64 u32 cluster_list_size;
65 u32 zone_list_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +010066};
67
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090068/**
Allan Stephensb52124a2011-05-30 09:44:38 -040069 * struct sub_seq - container for all published instances of a name sequence
70 * @lower: name sequence lower bound
71 * @upper: name sequence upper bound
72 * @info: pointer to name sequence publication info
73 */
Allan Stephensb52124a2011-05-30 09:44:38 -040074struct sub_seq {
75 u32 lower;
76 u32 upper;
77 struct name_info *info;
78};
79
80/**
Per Lidenb97bf3f2006-01-02 19:04:38 +010081 * struct name_seq - container for all published instances of a name type
82 * @type: 32 bit 'type' value for name sequence
83 * @sseq: pointer to dynamically-sized array of sub-sequences of this 'type';
84 * sub-sequences are sorted in ascending order
85 * @alloc: number of sub-sequences currently in array
Allan Stephensf1310722006-06-25 23:51:37 -070086 * @first_free: array index of first unused sub-sequence entry
Per Lidenb97bf3f2006-01-02 19:04:38 +010087 * @ns_list: links to adjacent name sequences in hash chain
88 * @subscriptions: list of subscriptions for this 'type'
Allan Stephens307fdf52008-06-04 17:38:22 -070089 * @lock: spinlock controlling access to publication lists of all sub-sequences
Per Lidenb97bf3f2006-01-02 19:04:38 +010090 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010091struct name_seq {
92 u32 type;
93 struct sub_seq *sseqs;
94 u32 alloc;
95 u32 first_free;
96 struct hlist_node ns_list;
97 struct list_head subscriptions;
98 spinlock_t lock;
99};
100
101/**
102 * struct name_table - table containing all existing port name publications
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900103 * @types: pointer to fixed-sized array of name sequence lists,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100104 * accessed via hashing on 'type'; name sequence lists are *not* sorted
105 * @local_publ_count: number of publications issued by this node
106 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100107struct name_table {
108 struct hlist_head *types;
109 u32 local_publ_count;
110};
111
Allan Stephense3ec9c72010-12-31 18:59:34 +0000112static struct name_table table;
Ingo Molnar34af9462006-06-27 02:53:55 -0700113DEFINE_RWLOCK(tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100114
Sam Ravnborg05790c62006-03-20 22:37:04 -0800115static int hash(int x)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100116{
Ying Xuef046e7d2012-08-16 12:09:11 +0000117 return x & (TIPC_NAMETBL_SIZE - 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100118}
119
120/**
121 * publ_create - create a publication structure
122 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900123static struct publication *publ_create(u32 type, u32 lower, u32 upper,
124 u32 scope, u32 node, u32 port_ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125 u32 key)
126{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700127 struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128 if (publ == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400129 pr_warn("Publication creation failure, no memory\n");
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800130 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100131 }
132
Per Lidenb97bf3f2006-01-02 19:04:38 +0100133 publ->type = type;
134 publ->lower = lower;
135 publ->upper = upper;
136 publ->scope = scope;
137 publ->node = node;
138 publ->ref = port_ref;
139 publ->key = key;
140 INIT_LIST_HEAD(&publ->local_list);
141 INIT_LIST_HEAD(&publ->pport_list);
142 INIT_LIST_HEAD(&publ->subscr.nodesub_list);
143 return publ;
144}
145
146/**
Per Liden4323add2006-01-18 00:38:21 +0100147 * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
Per Lidenb97bf3f2006-01-02 19:04:38 +0100148 */
Adrian Bunk988f0882006-03-20 22:37:52 -0800149static struct sub_seq *tipc_subseq_alloc(u32 cnt)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100150{
wangweidong0cee6bb2013-12-12 09:36:39 +0800151 return kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100152}
153
154/**
Per Liden4323add2006-01-18 00:38:21 +0100155 * tipc_nameseq_create - create a name sequence structure for the specified 'type'
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900156 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100157 * Allocates a single sub-sequence structure and sets it to all 0's.
158 */
Adrian Bunk988f0882006-03-20 22:37:52 -0800159static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160{
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700161 struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC);
Per Liden4323add2006-01-18 00:38:21 +0100162 struct sub_seq *sseq = tipc_subseq_alloc(1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100163
164 if (!nseq || !sseq) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400165 pr_warn("Name sequence creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166 kfree(nseq);
167 kfree(sseq);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800168 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169 }
170
Ingo Molnar34af9462006-06-27 02:53:55 -0700171 spin_lock_init(&nseq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100172 nseq->type = type;
173 nseq->sseqs = sseq;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100174 nseq->alloc = 1;
175 INIT_HLIST_NODE(&nseq->ns_list);
176 INIT_LIST_HEAD(&nseq->subscriptions);
177 hlist_add_head(&nseq->ns_list, seq_head);
178 return nseq;
179}
180
Allan Stephensf7fb9d22012-04-26 17:53:03 -0400181/*
182 * nameseq_delete_empty - deletes a name sequence structure if now unused
183 */
184static void nameseq_delete_empty(struct name_seq *seq)
185{
186 if (!seq->first_free && list_empty(&seq->subscriptions)) {
187 hlist_del_init(&seq->ns_list);
188 kfree(seq->sseqs);
189 kfree(seq);
190 }
191}
192
Ben Hutchings2c530402012-07-10 10:55:09 +0000193/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100194 * nameseq_find_subseq - find sub-sequence (if any) matching a name instance
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900195 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100196 * Very time-critical, so binary searches through sub-sequence array.
197 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800198static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq,
199 u32 instance)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100200{
201 struct sub_seq *sseqs = nseq->sseqs;
202 int low = 0;
203 int high = nseq->first_free - 1;
204 int mid;
205
206 while (low <= high) {
207 mid = (low + high) / 2;
208 if (instance < sseqs[mid].lower)
209 high = mid - 1;
210 else if (instance > sseqs[mid].upper)
211 low = mid + 1;
212 else
213 return &sseqs[mid];
214 }
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800215 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216}
217
218/**
219 * nameseq_locate_subseq - determine position of name instance in sub-sequence
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900220 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221 * Returns index in sub-sequence array of the entry that contains the specified
222 * instance value; if no entry contains that value, returns the position
223 * where a new entry for it would be inserted in the array.
224 *
225 * Note: Similar to binary search code for locating a sub-sequence.
226 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100227static 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/**
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400247 * tipc_nameseq_insert_publ
Per Lidenb97bf3f2006-01-02 19:04:38 +0100248 */
Adrian Bunk988f0882006-03-20 22:37:52 -0800249static struct publication *tipc_nameseq_insert_publ(struct name_seq *nseq,
250 u32 type, u32 lower, u32 upper,
251 u32 scope, u32 node, u32 port, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100252{
Paul Gortmakerfead3902011-12-29 20:43:44 -0500253 struct tipc_subscription *s;
254 struct tipc_subscription *st;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100255 struct publication *publ;
256 struct sub_seq *sseq;
Allan Stephensb52124a2011-05-30 09:44:38 -0400257 struct name_info *info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100258 int created_subseq = 0;
259
Per Lidenb97bf3f2006-01-02 19:04:38 +0100260 sseq = nameseq_find_subseq(nseq, lower);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100261 if (sseq) {
262
263 /* Lower end overlaps existing entry => need an exact match */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100264 if ((sseq->lower != lower) || (sseq->upper != upper)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400265 pr_warn("Cannot publish {%u,%u,%u}, overlap error\n",
266 type, lower, upper);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800267 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100268 }
Allan Stephensb52124a2011-05-30 09:44:38 -0400269
270 info = sseq->info;
Allan Stephensf80c24d2011-11-03 11:12:01 -0400271
272 /* Check if an identical publication already exists */
273 list_for_each_entry(publ, &info->zone_list, zone_list) {
274 if ((publ->ref == port) && (publ->key == key) &&
275 (!publ->node || (publ->node == node)))
276 return NULL;
277 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100278 } else {
279 u32 inspos;
280 struct sub_seq *freesseq;
281
282 /* Find where lower end should be inserted */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100283 inspos = nameseq_locate_subseq(nseq, lower);
284
285 /* Fail if upper end overlaps into an existing entry */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100286 if ((inspos < nseq->first_free) &&
287 (upper >= nseq->sseqs[inspos].lower)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400288 pr_warn("Cannot publish {%u,%u,%u}, overlap error\n",
289 type, lower, upper);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800290 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100291 }
292
293 /* Ensure there is space for new sub-sequence */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100294 if (nseq->first_free == nseq->alloc) {
Allan Stephens9ab230f2006-06-25 23:37:24 -0700295 struct sub_seq *sseqs = tipc_subseq_alloc(nseq->alloc * 2);
296
297 if (!sseqs) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400298 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
299 type, lower, upper);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800300 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100301 }
Allan Stephens9ab230f2006-06-25 23:37:24 -0700302 memcpy(sseqs, nseq->sseqs,
303 nseq->alloc * sizeof(struct sub_seq));
304 kfree(nseq->sseqs);
305 nseq->sseqs = sseqs;
306 nseq->alloc *= 2;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100307 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100308
Allan Stephensb52124a2011-05-30 09:44:38 -0400309 info = kzalloc(sizeof(*info), GFP_ATOMIC);
310 if (!info) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400311 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
312 type, lower, upper);
Allan Stephensb52124a2011-05-30 09:44:38 -0400313 return NULL;
314 }
315
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400316 INIT_LIST_HEAD(&info->node_list);
317 INIT_LIST_HEAD(&info->cluster_list);
318 INIT_LIST_HEAD(&info->zone_list);
319
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320 /* Insert new sub-sequence */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321 sseq = &nseq->sseqs[inspos];
322 freesseq = &nseq->sseqs[nseq->first_free];
Allan Stephens0e659672010-12-31 18:59:32 +0000323 memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof(*sseq));
324 memset(sseq, 0, sizeof(*sseq));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100325 nseq->first_free++;
326 sseq->lower = lower;
327 sseq->upper = upper;
Allan Stephensb52124a2011-05-30 09:44:38 -0400328 sseq->info = info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100329 created_subseq = 1;
330 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100331
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400332 /* Insert a publication */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333 publ = publ_create(type, lower, upper, scope, node, port, key);
334 if (!publ)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800335 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100336
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400337 list_add(&publ->zone_list, &info->zone_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400338 info->zone_list_size++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100339
Allan Stephensd4f5c122012-04-17 18:16:34 -0400340 if (in_own_cluster(node)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400341 list_add(&publ->cluster_list, &info->cluster_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400342 info->cluster_list_size++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100343 }
344
Allan Stephensd4f5c122012-04-17 18:16:34 -0400345 if (in_own_node(node)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400346 list_add(&publ->node_list, &info->node_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400347 info->node_list_size++;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100348 }
349
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400350 /* Any subscriptions waiting for notification? */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
Per Liden4323add2006-01-18 00:38:21 +0100352 tipc_subscr_report_overlap(s,
353 publ->lower,
354 publ->upper,
355 TIPC_PUBLISHED,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900356 publ->ref,
Per Liden4323add2006-01-18 00:38:21 +0100357 publ->node,
358 created_subseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359 }
360 return publ;
361}
362
363/**
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400364 * tipc_nameseq_remove_publ
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900365 *
Allan Stephensf1310722006-06-25 23:51:37 -0700366 * NOTE: There may be cases where TIPC is asked to remove a publication
367 * that is not in the name table. For example, if another node issues a
368 * publication for a name sequence that overlaps an existing name sequence
369 * the publication will not be recorded, which means the publication won't
370 * be found when the name sequence is later withdrawn by that node.
371 * A failed withdraw request simply returns a failure indication and lets the
372 * caller issue any error or warning messages associated with such a problem.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100373 */
Adrian Bunk988f0882006-03-20 22:37:52 -0800374static struct publication *tipc_nameseq_remove_publ(struct name_seq *nseq, u32 inst,
375 u32 node, u32 ref, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376{
377 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100378 struct sub_seq *sseq = nameseq_find_subseq(nseq, inst);
Allan Stephensb52124a2011-05-30 09:44:38 -0400379 struct name_info *info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100380 struct sub_seq *free;
Paul Gortmakerfead3902011-12-29 20:43:44 -0500381 struct tipc_subscription *s, *st;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100382 int removed_subseq = 0;
383
Allan Stephensf1310722006-06-25 23:51:37 -0700384 if (!sseq)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800385 return NULL;
Allan Stephensf1310722006-06-25 23:51:37 -0700386
Allan Stephensb52124a2011-05-30 09:44:38 -0400387 info = sseq->info;
388
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400389 /* Locate publication, if it exists */
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400390 list_for_each_entry(publ, &info->zone_list, zone_list) {
391 if ((publ->key == key) && (publ->ref == ref) &&
392 (!publ->node || (publ->node == node)))
393 goto found;
394 }
395 return NULL;
396
397found:
Allan Stephensf1310722006-06-25 23:51:37 -0700398 /* Remove publication from zone scope list */
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400399 list_del(&publ->zone_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400400 info->zone_list_size--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401
Allan Stephensf1310722006-06-25 23:51:37 -0700402 /* Remove publication from cluster scope list, if present */
Allan Stephensd4f5c122012-04-17 18:16:34 -0400403 if (in_own_cluster(node)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400404 list_del(&publ->cluster_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400405 info->cluster_list_size--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100406 }
Allan Stephensf1310722006-06-25 23:51:37 -0700407
408 /* Remove publication from node scope list, if present */
Allan Stephensd4f5c122012-04-17 18:16:34 -0400409 if (in_own_node(node)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400410 list_del(&publ->node_list);
Allan Stephensb52124a2011-05-30 09:44:38 -0400411 info->node_list_size--;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100412 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413
Allan Stephensf1310722006-06-25 23:51:37 -0700414 /* Contract subseq list if no more publications for that subseq */
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400415 if (list_empty(&info->zone_list)) {
Allan Stephensb52124a2011-05-30 09:44:38 -0400416 kfree(info);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417 free = &nseq->sseqs[nseq->first_free--];
Allan Stephens0e659672010-12-31 18:59:32 +0000418 memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100419 removed_subseq = 1;
420 }
421
Allan Stephensf1310722006-06-25 23:51:37 -0700422 /* Notify any waiting subscriptions */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
Per Liden4323add2006-01-18 00:38:21 +0100424 tipc_subscr_report_overlap(s,
425 publ->lower,
426 publ->upper,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900427 TIPC_WITHDRAWN,
428 publ->ref,
Per Liden4323add2006-01-18 00:38:21 +0100429 publ->node,
430 removed_subseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431 }
Allan Stephensf1310722006-06-25 23:51:37 -0700432
Per Lidenb97bf3f2006-01-02 19:04:38 +0100433 return publ;
434}
435
436/**
Ben Hutchings2c530402012-07-10 10:55:09 +0000437 * tipc_nameseq_subscribe - attach a subscription, and issue
Per Lidenb97bf3f2006-01-02 19:04:38 +0100438 * the prescribed number of events if there is any sub-
439 * sequence overlapping with the requested sequence
440 */
Paul Gortmakerfead3902011-12-29 20:43:44 -0500441static void tipc_nameseq_subscribe(struct name_seq *nseq,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400442 struct tipc_subscription *s)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100443{
444 struct sub_seq *sseq = nseq->sseqs;
445
446 list_add(&s->nameseq_list, &nseq->subscriptions);
447
448 if (!sseq)
449 return;
450
451 while (sseq != &nseq->sseqs[nseq->first_free]) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400452 if (tipc_subscr_overlap(s, sseq->lower, sseq->upper)) {
453 struct publication *crs;
454 struct name_info *info = sseq->info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100455 int must_report = 1;
456
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400457 list_for_each_entry(crs, &info->zone_list, zone_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900458 tipc_subscr_report_overlap(s,
459 sseq->lower,
Per Liden4323add2006-01-18 00:38:21 +0100460 sseq->upper,
461 TIPC_PUBLISHED,
462 crs->ref,
463 crs->node,
464 must_report);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465 must_report = 0;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400466 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100467 }
468 sseq++;
469 }
470}
471
472static struct name_seq *nametbl_find_seq(u32 type)
473{
474 struct hlist_head *seq_head;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100475 struct name_seq *ns;
476
Per Lidenb97bf3f2006-01-02 19:04:38 +0100477 seq_head = &table.types[hash(type)];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800478 hlist_for_each_entry(ns, seq_head, ns_list) {
Allan Stephensb29f1422010-12-31 18:59:25 +0000479 if (ns->type == type)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100480 return ns;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481 }
482
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800483 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100484};
485
Per Liden4323add2006-01-18 00:38:21 +0100486struct publication *tipc_nametbl_insert_publ(u32 type, u32 lower, u32 upper,
487 u32 scope, u32 node, u32 port, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488{
489 struct name_seq *seq = nametbl_find_seq(type);
490
Allan Stephens8f177892012-04-26 17:57:17 -0400491 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE) ||
492 (lower > upper)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400493 pr_debug("Failed to publish illegal {%u,%u,%u} with scope %u\n",
494 type, lower, upper, scope);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800495 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496 }
497
Allan Stephensb29f1422010-12-31 18:59:25 +0000498 if (!seq)
Per Liden4323add2006-01-18 00:38:21 +0100499 seq = tipc_nameseq_create(type, &table.types[hash(type)]);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100500 if (!seq)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800501 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100502
Per Liden4323add2006-01-18 00:38:21 +0100503 return tipc_nameseq_insert_publ(seq, type, lower, upper,
504 scope, node, port, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100505}
506
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900507struct publication *tipc_nametbl_remove_publ(u32 type, u32 lower,
Per Liden4323add2006-01-18 00:38:21 +0100508 u32 node, u32 ref, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509{
510 struct publication *publ;
511 struct name_seq *seq = nametbl_find_seq(type);
512
513 if (!seq)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800514 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100515
Per Liden4323add2006-01-18 00:38:21 +0100516 publ = tipc_nameseq_remove_publ(seq, lower, node, ref, key);
Allan Stephensf7fb9d22012-04-26 17:53:03 -0400517 nameseq_delete_empty(seq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100518 return publ;
519}
520
Ben Hutchings2c530402012-07-10 10:55:09 +0000521/**
Allan Stephensbc9f8142011-11-07 17:00:54 -0500522 * tipc_nametbl_translate - perform name translation
Per Lidenb97bf3f2006-01-02 19:04:38 +0100523 *
Allan Stephensbc9f8142011-11-07 17:00:54 -0500524 * On entry, 'destnode' is the search domain used during translation.
525 *
526 * On exit:
527 * - if name translation is deferred to another node/cluster/zone,
528 * leaves 'destnode' unchanged (will be non-zero) and returns 0
529 * - if name translation is attempted and succeeds, sets 'destnode'
530 * to publishing node and returns port reference (will be non-zero)
531 * - if name translation is attempted and fails, sets 'destnode' to 0
532 * and returns 0
Per Lidenb97bf3f2006-01-02 19:04:38 +0100533 */
Per Liden4323add2006-01-18 00:38:21 +0100534u32 tipc_nametbl_translate(u32 type, u32 instance, u32 *destnode)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535{
536 struct sub_seq *sseq;
Allan Stephensb52124a2011-05-30 09:44:38 -0400537 struct name_info *info;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400538 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100539 struct name_seq *seq;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400540 u32 ref = 0;
Allan Stephensbc9f8142011-11-07 17:00:54 -0500541 u32 node = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100542
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000543 if (!tipc_in_scope(*destnode, tipc_own_addr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544 return 0;
545
Per Liden4323add2006-01-18 00:38:21 +0100546 read_lock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547 seq = nametbl_find_seq(type);
548 if (unlikely(!seq))
549 goto not_found;
550 sseq = nameseq_find_subseq(seq, instance);
551 if (unlikely(!sseq))
552 goto not_found;
553 spin_lock_bh(&seq->lock);
Allan Stephensb52124a2011-05-30 09:44:38 -0400554 info = sseq->info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100555
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400556 /* Closest-First Algorithm */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557 if (likely(!*destnode)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400558 if (!list_empty(&info->node_list)) {
559 publ = list_first_entry(&info->node_list,
560 struct publication,
561 node_list);
562 list_move_tail(&publ->node_list,
563 &info->node_list);
564 } else if (!list_empty(&info->cluster_list)) {
565 publ = list_first_entry(&info->cluster_list,
566 struct publication,
567 cluster_list);
568 list_move_tail(&publ->cluster_list,
569 &info->cluster_list);
Allan Stephens8af46382011-05-30 11:27:50 -0400570 } else {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400571 publ = list_first_entry(&info->zone_list,
572 struct publication,
573 zone_list);
574 list_move_tail(&publ->zone_list,
575 &info->zone_list);
Allan Stephens8af46382011-05-30 11:27:50 -0400576 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100577 }
578
Paul Gortmaker617d3c72012-04-30 15:29:02 -0400579 /* Round-Robin Algorithm */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100580 else if (*destnode == tipc_own_addr) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400581 if (list_empty(&info->node_list))
582 goto no_match;
583 publ = list_first_entry(&info->node_list, struct publication,
584 node_list);
585 list_move_tail(&publ->node_list, &info->node_list);
Allan Stephens336ebf52012-04-17 18:02:01 -0400586 } else if (in_own_cluster_exact(*destnode)) {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400587 if (list_empty(&info->cluster_list))
588 goto no_match;
589 publ = list_first_entry(&info->cluster_list, struct publication,
590 cluster_list);
591 list_move_tail(&publ->cluster_list, &info->cluster_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100592 } else {
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400593 publ = list_first_entry(&info->zone_list, struct publication,
594 zone_list);
595 list_move_tail(&publ->zone_list, &info->zone_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100596 }
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400597
598 ref = publ->ref;
Allan Stephensbc9f8142011-11-07 17:00:54 -0500599 node = publ->node;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400600no_match:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100601 spin_unlock_bh(&seq->lock);
602not_found:
Per Liden4323add2006-01-18 00:38:21 +0100603 read_unlock_bh(&tipc_nametbl_lock);
Allan Stephensbc9f8142011-11-07 17:00:54 -0500604 *destnode = node;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400605 return ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606}
607
608/**
Per Liden4323add2006-01-18 00:38:21 +0100609 * tipc_nametbl_mc_translate - find multicast destinations
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900610 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100611 * Creates list of all local ports that overlap the given multicast address;
612 * also determines if any off-node ports overlap.
613 *
614 * Note: Publications with a scope narrower than 'limit' are ignored.
615 * (i.e. local node-scope publications mustn't receive messages arriving
616 * from another node, even if the multcast link brought it here)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900617 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100618 * Returns non-zero if any off-node ports overlap
619 */
Per Liden4323add2006-01-18 00:38:21 +0100620int tipc_nametbl_mc_translate(u32 type, u32 lower, u32 upper, u32 limit,
Paul Gortmaker45843102011-12-29 20:33:30 -0500621 struct tipc_port_list *dports)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100622{
623 struct name_seq *seq;
624 struct sub_seq *sseq;
625 struct sub_seq *sseq_stop;
Allan Stephensb52124a2011-05-30 09:44:38 -0400626 struct name_info *info;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100627 int res = 0;
628
Per Liden4323add2006-01-18 00:38:21 +0100629 read_lock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630 seq = nametbl_find_seq(type);
631 if (!seq)
632 goto exit;
633
634 spin_lock_bh(&seq->lock);
635
636 sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
637 sseq_stop = seq->sseqs + seq->first_free;
638 for (; sseq != sseq_stop; sseq++) {
639 struct publication *publ;
640
641 if (sseq->lower > upper)
642 break;
Allan Stephens968edbe2008-07-14 22:45:33 -0700643
Allan Stephensb52124a2011-05-30 09:44:38 -0400644 info = sseq->info;
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400645 list_for_each_entry(publ, &info->node_list, node_list) {
646 if (publ->scope <= limit)
647 tipc_port_list_add(dports, publ->ref);
Allan Stephens968edbe2008-07-14 22:45:33 -0700648 }
649
Allan Stephensb52124a2011-05-30 09:44:38 -0400650 if (info->cluster_list_size != info->node_list_size)
Allan Stephens968edbe2008-07-14 22:45:33 -0700651 res = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100652 }
653
654 spin_unlock_bh(&seq->lock);
655exit:
Per Liden4323add2006-01-18 00:38:21 +0100656 read_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657 return res;
658}
659
Allan Stephensc422f1b2011-11-02 15:49:40 -0400660/*
Per Liden4323add2006-01-18 00:38:21 +0100661 * tipc_nametbl_publish - add name publication to network name tables
Per Lidenb97bf3f2006-01-02 19:04:38 +0100662 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900663struct publication *tipc_nametbl_publish(u32 type, u32 lower, u32 upper,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400664 u32 scope, u32 port_ref, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100665{
666 struct publication *publ;
667
Ying Xuee6a04b12012-08-16 12:09:14 +0000668 if (table.local_publ_count >= TIPC_MAX_PUBLICATIONS) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400669 pr_warn("Publication failed, local publication limit reached (%u)\n",
Ying Xuee6a04b12012-08-16 12:09:14 +0000670 TIPC_MAX_PUBLICATIONS);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800671 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100672 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100673
Per Liden4323add2006-01-18 00:38:21 +0100674 write_lock_bh(&tipc_nametbl_lock);
Per Liden4323add2006-01-18 00:38:21 +0100675 publ = tipc_nametbl_insert_publ(type, lower, upper, scope,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676 tipc_own_addr, port_ref, key);
Allan Stephensfd6eced2011-11-09 14:22:52 -0500677 if (likely(publ)) {
678 table.local_publ_count++;
Per Liden4323add2006-01-18 00:38:21 +0100679 tipc_named_publish(publ);
Allan Stephensfd6eced2011-11-09 14:22:52 -0500680 }
Per Liden4323add2006-01-18 00:38:21 +0100681 write_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100682 return publ;
683}
684
685/**
Per Liden4323add2006-01-18 00:38:21 +0100686 * tipc_nametbl_withdraw - withdraw name publication from network name tables
Per Lidenb97bf3f2006-01-02 19:04:38 +0100687 */
Per Liden4323add2006-01-18 00:38:21 +0100688int tipc_nametbl_withdraw(u32 type, u32 lower, u32 ref, u32 key)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100689{
690 struct publication *publ;
691
Per Liden4323add2006-01-18 00:38:21 +0100692 write_lock_bh(&tipc_nametbl_lock);
693 publ = tipc_nametbl_remove_publ(type, lower, tipc_own_addr, ref, key);
Allan Stephensf1310722006-06-25 23:51:37 -0700694 if (likely(publ)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100695 table.local_publ_count--;
Allan Stephens1110b8d2012-04-17 17:57:52 -0400696 tipc_named_withdraw(publ);
Per Liden4323add2006-01-18 00:38:21 +0100697 write_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100698 list_del_init(&publ->pport_list);
699 kfree(publ);
700 return 1;
701 }
Per Liden4323add2006-01-18 00:38:21 +0100702 write_unlock_bh(&tipc_nametbl_lock);
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400703 pr_err("Unable to remove local publication\n"
704 "(type=%u, lower=%u, ref=%u, key=%u)\n",
705 type, lower, ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100706 return 0;
707}
708
709/**
Per Liden4323add2006-01-18 00:38:21 +0100710 * tipc_nametbl_subscribe - add a subscription object to the name table
Per Lidenb97bf3f2006-01-02 19:04:38 +0100711 */
Paul Gortmakerfead3902011-12-29 20:43:44 -0500712void tipc_nametbl_subscribe(struct tipc_subscription *s)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100713{
714 u32 type = s->seq.type;
715 struct name_seq *seq;
716
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900717 write_lock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100718 seq = nametbl_find_seq(type);
Allan Stephensa0168922010-12-31 18:59:35 +0000719 if (!seq)
Per Liden4323add2006-01-18 00:38:21 +0100720 seq = tipc_nameseq_create(type, &table.types[hash(type)]);
Allan Stephens0e659672010-12-31 18:59:32 +0000721 if (seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900722 spin_lock_bh(&seq->lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900723 tipc_nameseq_subscribe(seq, s);
724 spin_unlock_bh(&seq->lock);
725 } else {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400726 pr_warn("Failed to create subscription for {%u,%u,%u}\n",
727 s->seq.type, s->seq.lower, s->seq.upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900728 }
729 write_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100730}
731
732/**
Per Liden4323add2006-01-18 00:38:21 +0100733 * tipc_nametbl_unsubscribe - remove a subscription object from name table
Per Lidenb97bf3f2006-01-02 19:04:38 +0100734 */
Paul Gortmakerfead3902011-12-29 20:43:44 -0500735void tipc_nametbl_unsubscribe(struct tipc_subscription *s)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100736{
737 struct name_seq *seq;
738
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900739 write_lock_bh(&tipc_nametbl_lock);
740 seq = nametbl_find_seq(s->seq.type);
Allan Stephens0e659672010-12-31 18:59:32 +0000741 if (seq != NULL) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900742 spin_lock_bh(&seq->lock);
743 list_del_init(&s->nameseq_list);
744 spin_unlock_bh(&seq->lock);
Allan Stephensf7fb9d22012-04-26 17:53:03 -0400745 nameseq_delete_empty(seq);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900746 }
747 write_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100748}
749
750
751/**
Ben Hutchings2c530402012-07-10 10:55:09 +0000752 * subseq_list - print specified sub-sequence contents into the given buffer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100753 */
Erik Hugnedc1aed32012-06-29 00:50:23 -0400754static int subseq_list(struct sub_seq *sseq, char *buf, int len, u32 depth,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400755 u32 index)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100756{
757 char portIdStr[27];
Allan Stephensc2de5812010-08-17 11:00:14 +0000758 const char *scope_str[] = {"", " zone", " cluster", " node"};
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400759 struct publication *publ;
760 struct name_info *info;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400761 int ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100762
Erik Hugnedc1aed32012-06-29 00:50:23 -0400763 ret = tipc_snprintf(buf, len, "%-10u %-10u ", sseq->lower, sseq->upper);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100764
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400765 if (depth == 2) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400766 ret += tipc_snprintf(buf - ret, len + ret, "\n");
767 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100768 }
769
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400770 info = sseq->info;
771
772 list_for_each_entry(publ, &info->zone_list, zone_list) {
Allan Stephens0e659672010-12-31 18:59:32 +0000773 sprintf(portIdStr, "<%u.%u.%u:%u>",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100774 tipc_zone(publ->node), tipc_cluster(publ->node),
775 tipc_node(publ->node), publ->ref);
Erik Hugnedc1aed32012-06-29 00:50:23 -0400776 ret += tipc_snprintf(buf + ret, len - ret, "%-26s ", portIdStr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100777 if (depth > 3) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400778 ret += tipc_snprintf(buf + ret, len - ret, "%-10u %s",
779 publ->key, scope_str[publ->scope]);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100780 }
Allan Stephensf6f0a4d2011-05-30 10:48:48 -0400781 if (!list_is_last(&publ->zone_list, &info->zone_list))
Erik Hugnedc1aed32012-06-29 00:50:23 -0400782 ret += tipc_snprintf(buf + ret, len - ret,
783 "\n%33s", " ");
Peter Senna Tschudinadccff32012-09-18 07:10:45 +0000784 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100785
Erik Hugnedc1aed32012-06-29 00:50:23 -0400786 ret += tipc_snprintf(buf + ret, len - ret, "\n");
787 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100788}
789
790/**
Ben Hutchings2c530402012-07-10 10:55:09 +0000791 * nameseq_list - print specified name sequence contents into the given buffer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100792 */
Erik Hugnedc1aed32012-06-29 00:50:23 -0400793static int nameseq_list(struct name_seq *seq, char *buf, int len, u32 depth,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400794 u32 type, u32 lowbound, u32 upbound, u32 index)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100795{
796 struct sub_seq *sseq;
797 char typearea[11];
Erik Hugnedc1aed32012-06-29 00:50:23 -0400798 int ret = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100799
Allan Stephens0f15d362008-06-04 17:37:59 -0700800 if (seq->first_free == 0)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400801 return 0;
Allan Stephens0f15d362008-06-04 17:37:59 -0700802
Per Lidenb97bf3f2006-01-02 19:04:38 +0100803 sprintf(typearea, "%-10u", seq->type);
804
805 if (depth == 1) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400806 ret += tipc_snprintf(buf, len, "%s\n", typearea);
807 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100808 }
809
810 for (sseq = seq->sseqs; sseq != &seq->sseqs[seq->first_free]; sseq++) {
811 if ((lowbound <= sseq->upper) && (upbound >= sseq->lower)) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400812 ret += tipc_snprintf(buf + ret, len - ret, "%s ",
813 typearea);
Allan Stephens307fdf52008-06-04 17:38:22 -0700814 spin_lock_bh(&seq->lock);
Erik Hugnedc1aed32012-06-29 00:50:23 -0400815 ret += subseq_list(sseq, buf + ret, len - ret,
816 depth, index);
Allan Stephens307fdf52008-06-04 17:38:22 -0700817 spin_unlock_bh(&seq->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100818 sprintf(typearea, "%10s", " ");
819 }
820 }
Erik Hugnedc1aed32012-06-29 00:50:23 -0400821 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100822}
823
824/**
825 * nametbl_header - print name table header into the given buffer
826 */
Erik Hugnedc1aed32012-06-29 00:50:23 -0400827static int nametbl_header(char *buf, int len, u32 depth)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100828{
Allan Stephensc2de5812010-08-17 11:00:14 +0000829 const char *header[] = {
830 "Type ",
831 "Lower Upper ",
832 "Port Identity ",
833 "Publication Scope"
834 };
Per Lidenb97bf3f2006-01-02 19:04:38 +0100835
Allan Stephensc2de5812010-08-17 11:00:14 +0000836 int i;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400837 int ret = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100838
Allan Stephensc2de5812010-08-17 11:00:14 +0000839 if (depth > 4)
840 depth = 4;
841 for (i = 0; i < depth; i++)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400842 ret += tipc_snprintf(buf + ret, len - ret, header[i]);
843 ret += tipc_snprintf(buf + ret, len - ret, "\n");
844 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100845}
846
847/**
848 * nametbl_list - print specified name table contents into the given buffer
849 */
Erik Hugnedc1aed32012-06-29 00:50:23 -0400850static int nametbl_list(char *buf, int len, u32 depth_info,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400851 u32 type, u32 lowbound, u32 upbound)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100852{
853 struct hlist_head *seq_head;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100854 struct name_seq *seq;
855 int all_types;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400856 int ret = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100857 u32 depth;
858 u32 i;
859
860 all_types = (depth_info & TIPC_NTQ_ALLTYPES);
861 depth = (depth_info & ~TIPC_NTQ_ALLTYPES);
862
863 if (depth == 0)
Erik Hugnedc1aed32012-06-29 00:50:23 -0400864 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100865
866 if (all_types) {
867 /* display all entries in name table to specified depth */
Erik Hugnedc1aed32012-06-29 00:50:23 -0400868 ret += nametbl_header(buf, len, depth);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100869 lowbound = 0;
870 upbound = ~0;
Ying Xuef046e7d2012-08-16 12:09:11 +0000871 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100872 seq_head = &table.types[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800873 hlist_for_each_entry(seq, seq_head, ns_list) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400874 ret += nameseq_list(seq, buf + ret, len - ret,
875 depth, seq->type,
876 lowbound, upbound, i);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100877 }
878 }
879 } else {
880 /* display only the sequence that matches the specified type */
881 if (upbound < lowbound) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400882 ret += tipc_snprintf(buf + ret, len - ret,
883 "invalid name sequence specified\n");
884 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100885 }
Erik Hugnedc1aed32012-06-29 00:50:23 -0400886 ret += nametbl_header(buf + ret, len - ret, depth);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100887 i = hash(type);
888 seq_head = &table.types[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800889 hlist_for_each_entry(seq, seq_head, ns_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100890 if (seq->type == type) {
Erik Hugnedc1aed32012-06-29 00:50:23 -0400891 ret += nameseq_list(seq, buf + ret, len - ret,
892 depth, type,
893 lowbound, upbound, i);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100894 break;
895 }
896 }
897 }
Erik Hugnedc1aed32012-06-29 00:50:23 -0400898 return ret;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100899}
900
Per Liden4323add2006-01-18 00:38:21 +0100901struct sk_buff *tipc_nametbl_get(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100902{
903 struct sk_buff *buf;
904 struct tipc_name_table_query *argv;
905 struct tlv_desc *rep_tlv;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400906 char *pb;
907 int pb_len;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100908 int str_len;
909
910 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NAME_TBL_QUERY))
Per Liden4323add2006-01-18 00:38:21 +0100911 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100912
Erik Hugnedc1aed32012-06-29 00:50:23 -0400913 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100914 if (!buf)
915 return NULL;
916
917 rep_tlv = (struct tlv_desc *)buf->data;
Erik Hugnedc1aed32012-06-29 00:50:23 -0400918 pb = TLV_DATA(rep_tlv);
919 pb_len = ULTRA_STRING_MAX_LEN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100920 argv = (struct tipc_name_table_query *)TLV_DATA(req_tlv_area);
Per Liden4323add2006-01-18 00:38:21 +0100921 read_lock_bh(&tipc_nametbl_lock);
Erik Hugnedc1aed32012-06-29 00:50:23 -0400922 str_len = nametbl_list(pb, pb_len, ntohl(argv->depth),
923 ntohl(argv->type),
924 ntohl(argv->lowbound), ntohl(argv->upbound));
Per Liden4323add2006-01-18 00:38:21 +0100925 read_unlock_bh(&tipc_nametbl_lock);
Erik Hugnedc1aed32012-06-29 00:50:23 -0400926 str_len += 1; /* for "\0" */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100927 skb_put(buf, TLV_SPACE(str_len));
928 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
929
930 return buf;
931}
932
Per Liden4323add2006-01-18 00:38:21 +0100933int tipc_nametbl_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100934{
Ying Xuef046e7d2012-08-16 12:09:11 +0000935 table.types = kcalloc(TIPC_NAMETBL_SIZE, sizeof(struct hlist_head),
Allan Stephens4e3e6dc2008-05-12 15:41:53 -0700936 GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100937 if (!table.types)
938 return -ENOMEM;
939
Per Lidenb97bf3f2006-01-02 19:04:38 +0100940 table.local_publ_count = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100941 return 0;
942}
943
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100944/**
945 * tipc_purge_publications - remove all publications for a given type
946 *
947 * tipc_nametbl_lock must be held when calling this function
948 */
949static void tipc_purge_publications(struct name_seq *seq)
950{
951 struct publication *publ, *safe;
952 struct sub_seq *sseq;
953 struct name_info *info;
954
955 if (!seq->sseqs) {
956 nameseq_delete_empty(seq);
957 return;
958 }
959 sseq = seq->sseqs;
960 info = sseq->info;
961 list_for_each_entry_safe(publ, safe, &info->zone_list, zone_list) {
962 tipc_nametbl_remove_publ(publ->type, publ->lower, publ->node,
963 publ->ref, publ->key);
964 }
965}
966
Per Liden4323add2006-01-18 00:38:21 +0100967void tipc_nametbl_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100968{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100969 u32 i;
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100970 struct name_seq *seq;
971 struct hlist_head *seq_head;
972 struct hlist_node *safe;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100973
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100974 /* Verify name table is empty and purge any lingering
975 * publications, then release the name table
976 */
Per Liden4323add2006-01-18 00:38:21 +0100977 write_lock_bh(&tipc_nametbl_lock);
Ying Xuef046e7d2012-08-16 12:09:11 +0000978 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
Paul Gortmakerf705ab92012-07-11 17:35:01 -0400979 if (hlist_empty(&table.types[i]))
980 continue;
Erik Hugne1bb8dce2014-03-06 14:40:20 +0100981 seq_head = &table.types[i];
982 hlist_for_each_entry_safe(seq, safe, seq_head, ns_list) {
983 tipc_purge_publications(seq);
984 }
985 continue;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100986 }
987 kfree(table.types);
988 table.types = NULL;
Per Liden4323add2006-01-18 00:38:21 +0100989 write_unlock_bh(&tipc_nametbl_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100990}