Commit | Line | Data |
---|---|---|
b97bf3fd PL |
1 | /* |
2 | * net/tipc/name_table.c: TIPC name table code | |
c4307285 | 3 | * |
1593123a | 4 | * Copyright (c) 2000-2006, 2014, Ericsson AB |
993bfe5d | 5 | * Copyright (c) 2004-2008, 2010-2014, Wind River Systems |
b97bf3fd PL |
6 | * All rights reserved. |
7 | * | |
9ea1fd3c | 8 | * Redistribution and use in source and binary forms, with or without |
b97bf3fd PL |
9 | * modification, are permitted provided that the following conditions are met: |
10 | * | |
9ea1fd3c PL |
11 | * 1. Redistributions of source code must retain the above copyright |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * 2. Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in the | |
15 | * documentation and/or other materials provided with the distribution. | |
16 | * 3. Neither the names of the copyright holders nor the names of its | |
17 | * contributors may be used to endorse or promote products derived from | |
18 | * this software without specific prior written permission. | |
b97bf3fd | 19 | * |
9ea1fd3c PL |
20 | * Alternatively, this software may be distributed under the terms of the |
21 | * GNU General Public License ("GPL") version 2 as published by the Free | |
22 | * Software Foundation. | |
23 | * | |
24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
32 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
b97bf3fd PL |
34 | * POSSIBILITY OF SUCH DAMAGE. |
35 | */ | |
36 | ||
37 | #include "core.h" | |
38 | #include "config.h" | |
b97bf3fd PL |
39 | #include "name_table.h" |
40 | #include "name_distr.h" | |
b97bf3fd | 41 | #include "subscr.h" |
b97bf3fd | 42 | |
f046e7d9 | 43 | #define TIPC_NAMETBL_SIZE 1024 /* must be a power of 2 */ |
b97bf3fd | 44 | |
1593123a RA |
45 | static const struct nla_policy |
46 | tipc_nl_name_table_policy[TIPC_NLA_NAME_TABLE_MAX + 1] = { | |
47 | [TIPC_NLA_NAME_TABLE_UNSPEC] = { .type = NLA_UNSPEC }, | |
48 | [TIPC_NLA_NAME_TABLE_PUBL] = { .type = NLA_NESTED } | |
49 | }; | |
50 | ||
b97bf3fd | 51 | /** |
b52124a5 | 52 | * struct name_info - name sequence publication info |
968edbe1 AS |
53 | * @node_list: circular list of publications made by own node |
54 | * @cluster_list: circular list of publications made by own cluster | |
55 | * @zone_list: circular list of publications made by own zone | |
56 | * @node_list_size: number of entries in "node_list" | |
57 | * @cluster_list_size: number of entries in "cluster_list" | |
58 | * @zone_list_size: number of entries in "zone_list" | |
59 | * | |
60 | * Note: The zone list always contains at least one entry, since all | |
61 | * publications of the associated name sequence belong to it. | |
62 | * (The cluster and node lists may be empty.) | |
b97bf3fd | 63 | */ |
b52124a5 | 64 | struct name_info { |
f6f0a4d2 AS |
65 | struct list_head node_list; |
66 | struct list_head cluster_list; | |
67 | struct list_head zone_list; | |
968edbe1 AS |
68 | u32 node_list_size; |
69 | u32 cluster_list_size; | |
70 | u32 zone_list_size; | |
b97bf3fd PL |
71 | }; |
72 | ||
b52124a5 AS |
73 | /** |
74 | * struct sub_seq - container for all published instances of a name sequence | |
75 | * @lower: name sequence lower bound | |
76 | * @upper: name sequence upper bound | |
77 | * @info: pointer to name sequence publication info | |
78 | */ | |
b52124a5 AS |
79 | struct sub_seq { |
80 | u32 lower; | |
81 | u32 upper; | |
82 | struct name_info *info; | |
83 | }; | |
84 | ||
c4307285 | 85 | /** |
b97bf3fd PL |
86 | * struct name_seq - container for all published instances of a name type |
87 | * @type: 32 bit 'type' value for name sequence | |
88 | * @sseq: pointer to dynamically-sized array of sub-sequences of this 'type'; | |
89 | * sub-sequences are sorted in ascending order | |
90 | * @alloc: number of sub-sequences currently in array | |
f131072c | 91 | * @first_free: array index of first unused sub-sequence entry |
b97bf3fd PL |
92 | * @ns_list: links to adjacent name sequences in hash chain |
93 | * @subscriptions: list of subscriptions for this 'type' | |
307fdf5e | 94 | * @lock: spinlock controlling access to publication lists of all sub-sequences |
b97bf3fd | 95 | */ |
b97bf3fd PL |
96 | struct name_seq { |
97 | u32 type; | |
98 | struct sub_seq *sseqs; | |
99 | u32 alloc; | |
100 | u32 first_free; | |
101 | struct hlist_node ns_list; | |
102 | struct list_head subscriptions; | |
103 | spinlock_t lock; | |
104 | }; | |
105 | ||
993bfe5d | 106 | struct name_table *tipc_nametbl; |
34af946a | 107 | DEFINE_RWLOCK(tipc_nametbl_lock); |
b97bf3fd | 108 | |
05790c64 | 109 | static int hash(int x) |
b97bf3fd | 110 | { |
f046e7d9 | 111 | return x & (TIPC_NAMETBL_SIZE - 1); |
b97bf3fd PL |
112 | } |
113 | ||
114 | /** | |
115 | * publ_create - create a publication structure | |
116 | */ | |
c4307285 YH |
117 | static struct publication *publ_create(u32 type, u32 lower, u32 upper, |
118 | u32 scope, u32 node, u32 port_ref, | |
b97bf3fd PL |
119 | u32 key) |
120 | { | |
0da974f4 | 121 | struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC); |
b97bf3fd | 122 | if (publ == NULL) { |
2cf8aa19 | 123 | pr_warn("Publication creation failure, no memory\n"); |
1fc54d8f | 124 | return NULL; |
b97bf3fd PL |
125 | } |
126 | ||
b97bf3fd PL |
127 | publ->type = type; |
128 | publ->lower = lower; | |
129 | publ->upper = upper; | |
130 | publ->scope = scope; | |
131 | publ->node = node; | |
132 | publ->ref = port_ref; | |
133 | publ->key = key; | |
b97bf3fd | 134 | INIT_LIST_HEAD(&publ->pport_list); |
b97bf3fd PL |
135 | return publ; |
136 | } | |
137 | ||
138 | /** | |
4323add6 | 139 | * tipc_subseq_alloc - allocate a specified number of sub-sequence structures |
b97bf3fd | 140 | */ |
988f088a | 141 | static struct sub_seq *tipc_subseq_alloc(u32 cnt) |
b97bf3fd | 142 | { |
0cee6bbe | 143 | return kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC); |
b97bf3fd PL |
144 | } |
145 | ||
146 | /** | |
4323add6 | 147 | * tipc_nameseq_create - create a name sequence structure for the specified 'type' |
c4307285 | 148 | * |
b97bf3fd PL |
149 | * Allocates a single sub-sequence structure and sets it to all 0's. |
150 | */ | |
988f088a | 151 | static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head) |
b97bf3fd | 152 | { |
0da974f4 | 153 | struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC); |
4323add6 | 154 | struct sub_seq *sseq = tipc_subseq_alloc(1); |
b97bf3fd PL |
155 | |
156 | if (!nseq || !sseq) { | |
2cf8aa19 | 157 | pr_warn("Name sequence creation failed, no memory\n"); |
b97bf3fd PL |
158 | kfree(nseq); |
159 | kfree(sseq); | |
1fc54d8f | 160 | return NULL; |
b97bf3fd PL |
161 | } |
162 | ||
34af946a | 163 | spin_lock_init(&nseq->lock); |
b97bf3fd PL |
164 | nseq->type = type; |
165 | nseq->sseqs = sseq; | |
b97bf3fd PL |
166 | nseq->alloc = 1; |
167 | INIT_HLIST_NODE(&nseq->ns_list); | |
168 | INIT_LIST_HEAD(&nseq->subscriptions); | |
169 | hlist_add_head(&nseq->ns_list, seq_head); | |
170 | return nseq; | |
171 | } | |
172 | ||
2c53040f | 173 | /** |
b97bf3fd | 174 | * nameseq_find_subseq - find sub-sequence (if any) matching a name instance |
c4307285 | 175 | * |
b97bf3fd PL |
176 | * Very time-critical, so binary searches through sub-sequence array. |
177 | */ | |
05790c64 SR |
178 | static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq, |
179 | u32 instance) | |
b97bf3fd PL |
180 | { |
181 | struct sub_seq *sseqs = nseq->sseqs; | |
182 | int low = 0; | |
183 | int high = nseq->first_free - 1; | |
184 | int mid; | |
185 | ||
186 | while (low <= high) { | |
187 | mid = (low + high) / 2; | |
188 | if (instance < sseqs[mid].lower) | |
189 | high = mid - 1; | |
190 | else if (instance > sseqs[mid].upper) | |
191 | low = mid + 1; | |
192 | else | |
193 | return &sseqs[mid]; | |
194 | } | |
1fc54d8f | 195 | return NULL; |
b97bf3fd PL |
196 | } |
197 | ||
198 | /** | |
199 | * nameseq_locate_subseq - determine position of name instance in sub-sequence | |
c4307285 | 200 | * |
b97bf3fd PL |
201 | * Returns index in sub-sequence array of the entry that contains the specified |
202 | * instance value; if no entry contains that value, returns the position | |
203 | * where a new entry for it would be inserted in the array. | |
204 | * | |
205 | * Note: Similar to binary search code for locating a sub-sequence. | |
206 | */ | |
b97bf3fd PL |
207 | static u32 nameseq_locate_subseq(struct name_seq *nseq, u32 instance) |
208 | { | |
209 | struct sub_seq *sseqs = nseq->sseqs; | |
210 | int low = 0; | |
211 | int high = nseq->first_free - 1; | |
212 | int mid; | |
213 | ||
214 | while (low <= high) { | |
215 | mid = (low + high) / 2; | |
216 | if (instance < sseqs[mid].lower) | |
217 | high = mid - 1; | |
218 | else if (instance > sseqs[mid].upper) | |
219 | low = mid + 1; | |
220 | else | |
221 | return mid; | |
222 | } | |
223 | return low; | |
224 | } | |
225 | ||
226 | /** | |
617d3c7a | 227 | * tipc_nameseq_insert_publ |
b97bf3fd | 228 | */ |
988f088a AB |
229 | static struct publication *tipc_nameseq_insert_publ(struct name_seq *nseq, |
230 | u32 type, u32 lower, u32 upper, | |
231 | u32 scope, u32 node, u32 port, u32 key) | |
b97bf3fd | 232 | { |
fead3909 PG |
233 | struct tipc_subscription *s; |
234 | struct tipc_subscription *st; | |
b97bf3fd PL |
235 | struct publication *publ; |
236 | struct sub_seq *sseq; | |
b52124a5 | 237 | struct name_info *info; |
b97bf3fd PL |
238 | int created_subseq = 0; |
239 | ||
b97bf3fd | 240 | sseq = nameseq_find_subseq(nseq, lower); |
b97bf3fd PL |
241 | if (sseq) { |
242 | ||
243 | /* Lower end overlaps existing entry => need an exact match */ | |
b97bf3fd | 244 | if ((sseq->lower != lower) || (sseq->upper != upper)) { |
1fc54d8f | 245 | return NULL; |
b97bf3fd | 246 | } |
b52124a5 AS |
247 | |
248 | info = sseq->info; | |
f80c24d9 AS |
249 | |
250 | /* Check if an identical publication already exists */ | |
251 | list_for_each_entry(publ, &info->zone_list, zone_list) { | |
252 | if ((publ->ref == port) && (publ->key == key) && | |
253 | (!publ->node || (publ->node == node))) | |
254 | return NULL; | |
255 | } | |
b97bf3fd PL |
256 | } else { |
257 | u32 inspos; | |
258 | struct sub_seq *freesseq; | |
259 | ||
260 | /* Find where lower end should be inserted */ | |
b97bf3fd PL |
261 | inspos = nameseq_locate_subseq(nseq, lower); |
262 | ||
263 | /* Fail if upper end overlaps into an existing entry */ | |
b97bf3fd PL |
264 | if ((inspos < nseq->first_free) && |
265 | (upper >= nseq->sseqs[inspos].lower)) { | |
1fc54d8f | 266 | return NULL; |
b97bf3fd PL |
267 | } |
268 | ||
269 | /* Ensure there is space for new sub-sequence */ | |
b97bf3fd | 270 | if (nseq->first_free == nseq->alloc) { |
9ab230f8 AS |
271 | struct sub_seq *sseqs = tipc_subseq_alloc(nseq->alloc * 2); |
272 | ||
273 | if (!sseqs) { | |
2cf8aa19 EH |
274 | pr_warn("Cannot publish {%u,%u,%u}, no memory\n", |
275 | type, lower, upper); | |
1fc54d8f | 276 | return NULL; |
b97bf3fd | 277 | } |
9ab230f8 AS |
278 | memcpy(sseqs, nseq->sseqs, |
279 | nseq->alloc * sizeof(struct sub_seq)); | |
280 | kfree(nseq->sseqs); | |
281 | nseq->sseqs = sseqs; | |
282 | nseq->alloc *= 2; | |
b97bf3fd | 283 | } |
b97bf3fd | 284 | |
b52124a5 AS |
285 | info = kzalloc(sizeof(*info), GFP_ATOMIC); |
286 | if (!info) { | |
2cf8aa19 EH |
287 | pr_warn("Cannot publish {%u,%u,%u}, no memory\n", |
288 | type, lower, upper); | |
b52124a5 AS |
289 | return NULL; |
290 | } | |
291 | ||
f6f0a4d2 AS |
292 | INIT_LIST_HEAD(&info->node_list); |
293 | INIT_LIST_HEAD(&info->cluster_list); | |
294 | INIT_LIST_HEAD(&info->zone_list); | |
295 | ||
b97bf3fd | 296 | /* Insert new sub-sequence */ |
b97bf3fd PL |
297 | sseq = &nseq->sseqs[inspos]; |
298 | freesseq = &nseq->sseqs[nseq->first_free]; | |
0e65967e AS |
299 | memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof(*sseq)); |
300 | memset(sseq, 0, sizeof(*sseq)); | |
b97bf3fd PL |
301 | nseq->first_free++; |
302 | sseq->lower = lower; | |
303 | sseq->upper = upper; | |
b52124a5 | 304 | sseq->info = info; |
b97bf3fd PL |
305 | created_subseq = 1; |
306 | } | |
b97bf3fd | 307 | |
617d3c7a | 308 | /* Insert a publication */ |
b97bf3fd PL |
309 | publ = publ_create(type, lower, upper, scope, node, port, key); |
310 | if (!publ) | |
1fc54d8f | 311 | return NULL; |
b97bf3fd | 312 | |
f6f0a4d2 | 313 | list_add(&publ->zone_list, &info->zone_list); |
b52124a5 | 314 | info->zone_list_size++; |
b97bf3fd | 315 | |
d4f5c12c | 316 | if (in_own_cluster(node)) { |
f6f0a4d2 | 317 | list_add(&publ->cluster_list, &info->cluster_list); |
b52124a5 | 318 | info->cluster_list_size++; |
b97bf3fd PL |
319 | } |
320 | ||
d4f5c12c | 321 | if (in_own_node(node)) { |
f6f0a4d2 | 322 | list_add(&publ->node_list, &info->node_list); |
b52124a5 | 323 | info->node_list_size++; |
b97bf3fd PL |
324 | } |
325 | ||
617d3c7a | 326 | /* Any subscriptions waiting for notification? */ |
b97bf3fd | 327 | list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) { |
4323add6 PL |
328 | tipc_subscr_report_overlap(s, |
329 | publ->lower, | |
330 | publ->upper, | |
331 | TIPC_PUBLISHED, | |
c4307285 | 332 | publ->ref, |
4323add6 PL |
333 | publ->node, |
334 | created_subseq); | |
b97bf3fd PL |
335 | } |
336 | return publ; | |
337 | } | |
338 | ||
339 | /** | |
617d3c7a | 340 | * tipc_nameseq_remove_publ |
c4307285 | 341 | * |
f131072c AS |
342 | * NOTE: There may be cases where TIPC is asked to remove a publication |
343 | * that is not in the name table. For example, if another node issues a | |
344 | * publication for a name sequence that overlaps an existing name sequence | |
345 | * the publication will not be recorded, which means the publication won't | |
346 | * be found when the name sequence is later withdrawn by that node. | |
347 | * A failed withdraw request simply returns a failure indication and lets the | |
348 | * caller issue any error or warning messages associated with such a problem. | |
b97bf3fd | 349 | */ |
988f088a AB |
350 | static struct publication *tipc_nameseq_remove_publ(struct name_seq *nseq, u32 inst, |
351 | u32 node, u32 ref, u32 key) | |
b97bf3fd PL |
352 | { |
353 | struct publication *publ; | |
b97bf3fd | 354 | struct sub_seq *sseq = nameseq_find_subseq(nseq, inst); |
b52124a5 | 355 | struct name_info *info; |
b97bf3fd | 356 | struct sub_seq *free; |
fead3909 | 357 | struct tipc_subscription *s, *st; |
b97bf3fd PL |
358 | int removed_subseq = 0; |
359 | ||
f131072c | 360 | if (!sseq) |
1fc54d8f | 361 | return NULL; |
f131072c | 362 | |
b52124a5 AS |
363 | info = sseq->info; |
364 | ||
f6f0a4d2 | 365 | /* Locate publication, if it exists */ |
f6f0a4d2 AS |
366 | list_for_each_entry(publ, &info->zone_list, zone_list) { |
367 | if ((publ->key == key) && (publ->ref == ref) && | |
368 | (!publ->node || (publ->node == node))) | |
369 | goto found; | |
370 | } | |
371 | return NULL; | |
c4307285 | 372 | |
f6f0a4d2 AS |
373 | found: |
374 | /* Remove publication from zone scope list */ | |
f6f0a4d2 | 375 | list_del(&publ->zone_list); |
b52124a5 | 376 | info->zone_list_size--; |
b97bf3fd | 377 | |
f131072c | 378 | /* Remove publication from cluster scope list, if present */ |
d4f5c12c | 379 | if (in_own_cluster(node)) { |
f6f0a4d2 | 380 | list_del(&publ->cluster_list); |
b52124a5 | 381 | info->cluster_list_size--; |
b97bf3fd | 382 | } |
f131072c AS |
383 | |
384 | /* Remove publication from node scope list, if present */ | |
d4f5c12c | 385 | if (in_own_node(node)) { |
f6f0a4d2 | 386 | list_del(&publ->node_list); |
b52124a5 | 387 | info->node_list_size--; |
b97bf3fd | 388 | } |
b97bf3fd | 389 | |
f131072c | 390 | /* Contract subseq list if no more publications for that subseq */ |
f6f0a4d2 | 391 | if (list_empty(&info->zone_list)) { |
b52124a5 | 392 | kfree(info); |
b97bf3fd | 393 | free = &nseq->sseqs[nseq->first_free--]; |
0e65967e | 394 | memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq)); |
b97bf3fd PL |
395 | removed_subseq = 1; |
396 | } | |
397 | ||
f131072c | 398 | /* Notify any waiting subscriptions */ |
b97bf3fd | 399 | list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) { |
4323add6 PL |
400 | tipc_subscr_report_overlap(s, |
401 | publ->lower, | |
402 | publ->upper, | |
c4307285 YH |
403 | TIPC_WITHDRAWN, |
404 | publ->ref, | |
4323add6 PL |
405 | publ->node, |
406 | removed_subseq); | |
b97bf3fd | 407 | } |
f131072c | 408 | |
b97bf3fd PL |
409 | return publ; |
410 | } | |
411 | ||
412 | /** | |
2c53040f | 413 | * tipc_nameseq_subscribe - attach a subscription, and issue |
b97bf3fd PL |
414 | * the prescribed number of events if there is any sub- |
415 | * sequence overlapping with the requested sequence | |
416 | */ | |
fead3909 | 417 | static void tipc_nameseq_subscribe(struct name_seq *nseq, |
ae8509c4 | 418 | struct tipc_subscription *s) |
b97bf3fd PL |
419 | { |
420 | struct sub_seq *sseq = nseq->sseqs; | |
421 | ||
422 | list_add(&s->nameseq_list, &nseq->subscriptions); | |
423 | ||
424 | if (!sseq) | |
425 | return; | |
426 | ||
427 | while (sseq != &nseq->sseqs[nseq->first_free]) { | |
f6f0a4d2 AS |
428 | if (tipc_subscr_overlap(s, sseq->lower, sseq->upper)) { |
429 | struct publication *crs; | |
430 | struct name_info *info = sseq->info; | |
b97bf3fd PL |
431 | int must_report = 1; |
432 | ||
f6f0a4d2 | 433 | list_for_each_entry(crs, &info->zone_list, zone_list) { |
c4307285 YH |
434 | tipc_subscr_report_overlap(s, |
435 | sseq->lower, | |
4323add6 PL |
436 | sseq->upper, |
437 | TIPC_PUBLISHED, | |
438 | crs->ref, | |
439 | crs->node, | |
440 | must_report); | |
b97bf3fd | 441 | must_report = 0; |
f6f0a4d2 | 442 | } |
b97bf3fd PL |
443 | } |
444 | sseq++; | |
445 | } | |
446 | } | |
447 | ||
448 | static struct name_seq *nametbl_find_seq(u32 type) | |
449 | { | |
450 | struct hlist_head *seq_head; | |
b97bf3fd PL |
451 | struct name_seq *ns; |
452 | ||
993bfe5d | 453 | seq_head = &tipc_nametbl->seq_hlist[hash(type)]; |
b67bfe0d | 454 | hlist_for_each_entry(ns, seq_head, ns_list) { |
b29f1428 | 455 | if (ns->type == type) |
b97bf3fd | 456 | return ns; |
b97bf3fd PL |
457 | } |
458 | ||
1fc54d8f | 459 | return NULL; |
b97bf3fd PL |
460 | }; |
461 | ||
4323add6 PL |
462 | struct publication *tipc_nametbl_insert_publ(u32 type, u32 lower, u32 upper, |
463 | u32 scope, u32 node, u32 port, u32 key) | |
b97bf3fd | 464 | { |
fb9962f3 | 465 | struct publication *publ; |
b97bf3fd | 466 | struct name_seq *seq = nametbl_find_seq(type); |
993bfe5d | 467 | int index = hash(type); |
b97bf3fd | 468 | |
8f177896 AS |
469 | if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE) || |
470 | (lower > upper)) { | |
2cf8aa19 EH |
471 | pr_debug("Failed to publish illegal {%u,%u,%u} with scope %u\n", |
472 | type, lower, upper, scope); | |
1fc54d8f | 473 | return NULL; |
b97bf3fd PL |
474 | } |
475 | ||
b29f1428 | 476 | if (!seq) |
993bfe5d YX |
477 | seq = tipc_nameseq_create(type, |
478 | &tipc_nametbl->seq_hlist[index]); | |
b97bf3fd | 479 | if (!seq) |
1fc54d8f | 480 | return NULL; |
b97bf3fd | 481 | |
fb9962f3 YX |
482 | spin_lock_bh(&seq->lock); |
483 | publ = tipc_nameseq_insert_publ(seq, type, lower, upper, | |
4323add6 | 484 | scope, node, port, key); |
fb9962f3 YX |
485 | spin_unlock_bh(&seq->lock); |
486 | return publ; | |
b97bf3fd PL |
487 | } |
488 | ||
c4307285 | 489 | struct publication *tipc_nametbl_remove_publ(u32 type, u32 lower, |
4323add6 | 490 | u32 node, u32 ref, u32 key) |
b97bf3fd PL |
491 | { |
492 | struct publication *publ; | |
493 | struct name_seq *seq = nametbl_find_seq(type); | |
494 | ||
495 | if (!seq) | |
1fc54d8f | 496 | return NULL; |
b97bf3fd | 497 | |
fb9962f3 | 498 | spin_lock_bh(&seq->lock); |
4323add6 | 499 | publ = tipc_nameseq_remove_publ(seq, lower, node, ref, key); |
fb9962f3 YX |
500 | if (!seq->first_free && list_empty(&seq->subscriptions)) { |
501 | hlist_del_init(&seq->ns_list); | |
502 | spin_unlock_bh(&seq->lock); | |
503 | kfree(seq->sseqs); | |
504 | kfree(seq); | |
505 | return publ; | |
506 | } | |
507 | spin_unlock_bh(&seq->lock); | |
b97bf3fd PL |
508 | return publ; |
509 | } | |
510 | ||
2c53040f | 511 | /** |
bc9f8143 | 512 | * tipc_nametbl_translate - perform name translation |
b97bf3fd | 513 | * |
bc9f8143 AS |
514 | * On entry, 'destnode' is the search domain used during translation. |
515 | * | |
516 | * On exit: | |
517 | * - if name translation is deferred to another node/cluster/zone, | |
518 | * leaves 'destnode' unchanged (will be non-zero) and returns 0 | |
519 | * - if name translation is attempted and succeeds, sets 'destnode' | |
520 | * to publishing node and returns port reference (will be non-zero) | |
521 | * - if name translation is attempted and fails, sets 'destnode' to 0 | |
522 | * and returns 0 | |
b97bf3fd | 523 | */ |
4323add6 | 524 | u32 tipc_nametbl_translate(u32 type, u32 instance, u32 *destnode) |
b97bf3fd PL |
525 | { |
526 | struct sub_seq *sseq; | |
b52124a5 | 527 | struct name_info *info; |
f6f0a4d2 | 528 | struct publication *publ; |
b97bf3fd | 529 | struct name_seq *seq; |
f6f0a4d2 | 530 | u32 ref = 0; |
bc9f8143 | 531 | u32 node = 0; |
b97bf3fd | 532 | |
c68ca7b7 | 533 | if (!tipc_in_scope(*destnode, tipc_own_addr)) |
b97bf3fd PL |
534 | return 0; |
535 | ||
4323add6 | 536 | read_lock_bh(&tipc_nametbl_lock); |
b97bf3fd PL |
537 | seq = nametbl_find_seq(type); |
538 | if (unlikely(!seq)) | |
539 | goto not_found; | |
fb9962f3 | 540 | spin_lock_bh(&seq->lock); |
b97bf3fd PL |
541 | sseq = nameseq_find_subseq(seq, instance); |
542 | if (unlikely(!sseq)) | |
fb9962f3 | 543 | goto no_match; |
b52124a5 | 544 | info = sseq->info; |
b97bf3fd | 545 | |
617d3c7a | 546 | /* Closest-First Algorithm */ |
b97bf3fd | 547 | if (likely(!*destnode)) { |
f6f0a4d2 AS |
548 | if (!list_empty(&info->node_list)) { |
549 | publ = list_first_entry(&info->node_list, | |
550 | struct publication, | |
551 | node_list); | |
552 | list_move_tail(&publ->node_list, | |
553 | &info->node_list); | |
554 | } else if (!list_empty(&info->cluster_list)) { | |
555 | publ = list_first_entry(&info->cluster_list, | |
556 | struct publication, | |
557 | cluster_list); | |
558 | list_move_tail(&publ->cluster_list, | |
559 | &info->cluster_list); | |
8af4638a | 560 | } else { |
f6f0a4d2 AS |
561 | publ = list_first_entry(&info->zone_list, |
562 | struct publication, | |
563 | zone_list); | |
564 | list_move_tail(&publ->zone_list, | |
565 | &info->zone_list); | |
8af4638a | 566 | } |
b97bf3fd PL |
567 | } |
568 | ||
617d3c7a | 569 | /* Round-Robin Algorithm */ |
b97bf3fd | 570 | else if (*destnode == tipc_own_addr) { |
f6f0a4d2 AS |
571 | if (list_empty(&info->node_list)) |
572 | goto no_match; | |
573 | publ = list_first_entry(&info->node_list, struct publication, | |
574 | node_list); | |
575 | list_move_tail(&publ->node_list, &info->node_list); | |
336ebf5b | 576 | } else if (in_own_cluster_exact(*destnode)) { |
f6f0a4d2 AS |
577 | if (list_empty(&info->cluster_list)) |
578 | goto no_match; | |
579 | publ = list_first_entry(&info->cluster_list, struct publication, | |
580 | cluster_list); | |
581 | list_move_tail(&publ->cluster_list, &info->cluster_list); | |
b97bf3fd | 582 | } else { |
f6f0a4d2 AS |
583 | publ = list_first_entry(&info->zone_list, struct publication, |
584 | zone_list); | |
585 | list_move_tail(&publ->zone_list, &info->zone_list); | |
b97bf3fd | 586 | } |
f6f0a4d2 AS |
587 | |
588 | ref = publ->ref; | |
bc9f8143 | 589 | node = publ->node; |
f6f0a4d2 | 590 | no_match: |
b97bf3fd PL |
591 | spin_unlock_bh(&seq->lock); |
592 | not_found: | |
4323add6 | 593 | read_unlock_bh(&tipc_nametbl_lock); |
bc9f8143 | 594 | *destnode = node; |
f6f0a4d2 | 595 | return ref; |
b97bf3fd PL |
596 | } |
597 | ||
598 | /** | |
4323add6 | 599 | * tipc_nametbl_mc_translate - find multicast destinations |
c4307285 | 600 | * |
b97bf3fd PL |
601 | * Creates list of all local ports that overlap the given multicast address; |
602 | * also determines if any off-node ports overlap. | |
603 | * | |
604 | * Note: Publications with a scope narrower than 'limit' are ignored. | |
605 | * (i.e. local node-scope publications mustn't receive messages arriving | |
606 | * from another node, even if the multcast link brought it here) | |
c4307285 | 607 | * |
b97bf3fd PL |
608 | * Returns non-zero if any off-node ports overlap |
609 | */ | |
4323add6 | 610 | int tipc_nametbl_mc_translate(u32 type, u32 lower, u32 upper, u32 limit, |
4584310b | 611 | struct tipc_port_list *dports) |
b97bf3fd PL |
612 | { |
613 | struct name_seq *seq; | |
614 | struct sub_seq *sseq; | |
615 | struct sub_seq *sseq_stop; | |
b52124a5 | 616 | struct name_info *info; |
b97bf3fd PL |
617 | int res = 0; |
618 | ||
4323add6 | 619 | read_lock_bh(&tipc_nametbl_lock); |
b97bf3fd PL |
620 | seq = nametbl_find_seq(type); |
621 | if (!seq) | |
622 | goto exit; | |
623 | ||
624 | spin_lock_bh(&seq->lock); | |
b97bf3fd PL |
625 | sseq = seq->sseqs + nameseq_locate_subseq(seq, lower); |
626 | sseq_stop = seq->sseqs + seq->first_free; | |
627 | for (; sseq != sseq_stop; sseq++) { | |
628 | struct publication *publ; | |
629 | ||
630 | if (sseq->lower > upper) | |
631 | break; | |
968edbe1 | 632 | |
b52124a5 | 633 | info = sseq->info; |
f6f0a4d2 AS |
634 | list_for_each_entry(publ, &info->node_list, node_list) { |
635 | if (publ->scope <= limit) | |
636 | tipc_port_list_add(dports, publ->ref); | |
968edbe1 AS |
637 | } |
638 | ||
b52124a5 | 639 | if (info->cluster_list_size != info->node_list_size) |
968edbe1 | 640 | res = 1; |
b97bf3fd | 641 | } |
b97bf3fd PL |
642 | spin_unlock_bh(&seq->lock); |
643 | exit: | |
4323add6 | 644 | read_unlock_bh(&tipc_nametbl_lock); |
b97bf3fd PL |
645 | return res; |
646 | } | |
647 | ||
c422f1bd | 648 | /* |
4323add6 | 649 | * tipc_nametbl_publish - add name publication to network name tables |
b97bf3fd | 650 | */ |
c4307285 | 651 | struct publication *tipc_nametbl_publish(u32 type, u32 lower, u32 upper, |
ae8509c4 | 652 | u32 scope, u32 port_ref, u32 key) |
b97bf3fd PL |
653 | { |
654 | struct publication *publ; | |
eab8c045 | 655 | struct sk_buff *buf = NULL; |
b97bf3fd | 656 | |
3493d25c | 657 | write_lock_bh(&tipc_nametbl_lock); |
993bfe5d | 658 | if (tipc_nametbl->local_publ_count >= TIPC_MAX_PUBLICATIONS) { |
2cf8aa19 | 659 | pr_warn("Publication failed, local publication limit reached (%u)\n", |
e6a04b1d | 660 | TIPC_MAX_PUBLICATIONS); |
3493d25c | 661 | write_unlock_bh(&tipc_nametbl_lock); |
1fc54d8f | 662 | return NULL; |
b97bf3fd | 663 | } |
b97bf3fd | 664 | |
4323add6 | 665 | publ = tipc_nametbl_insert_publ(type, lower, upper, scope, |
b97bf3fd | 666 | tipc_own_addr, port_ref, key); |
fd6eced8 | 667 | if (likely(publ)) { |
993bfe5d | 668 | tipc_nametbl->local_publ_count++; |
eab8c045 | 669 | buf = tipc_named_publish(publ); |
a5325ae5 EH |
670 | /* Any pending external events? */ |
671 | tipc_named_process_backlog(); | |
fd6eced8 | 672 | } |
4323add6 | 673 | write_unlock_bh(&tipc_nametbl_lock); |
eab8c045 YX |
674 | |
675 | if (buf) | |
676 | named_cluster_distribute(buf); | |
b97bf3fd PL |
677 | return publ; |
678 | } | |
679 | ||
680 | /** | |
4323add6 | 681 | * tipc_nametbl_withdraw - withdraw name publication from network name tables |
b97bf3fd | 682 | */ |
4323add6 | 683 | int tipc_nametbl_withdraw(u32 type, u32 lower, u32 ref, u32 key) |
b97bf3fd PL |
684 | { |
685 | struct publication *publ; | |
5492390a | 686 | struct sk_buff *skb = NULL; |
b97bf3fd | 687 | |
4323add6 PL |
688 | write_lock_bh(&tipc_nametbl_lock); |
689 | publ = tipc_nametbl_remove_publ(type, lower, tipc_own_addr, ref, key); | |
f131072c | 690 | if (likely(publ)) { |
993bfe5d | 691 | tipc_nametbl->local_publ_count--; |
5492390a | 692 | skb = tipc_named_withdraw(publ); |
a5325ae5 EH |
693 | /* Any pending external events? */ |
694 | tipc_named_process_backlog(); | |
b97bf3fd PL |
695 | list_del_init(&publ->pport_list); |
696 | kfree(publ); | |
5492390a YX |
697 | } else { |
698 | pr_err("Unable to remove local publication\n" | |
699 | "(type=%u, lower=%u, ref=%u, key=%u)\n", | |
700 | type, lower, ref, key); | |
701 | } | |
702 | write_unlock_bh(&tipc_nametbl_lock); | |
eab8c045 | 703 | |
5492390a YX |
704 | if (skb) { |
705 | named_cluster_distribute(skb); | |
b97bf3fd PL |
706 | return 1; |
707 | } | |
b97bf3fd PL |
708 | return 0; |
709 | } | |
710 | ||
711 | /** | |
4323add6 | 712 | * tipc_nametbl_subscribe - add a subscription object to the name table |
b97bf3fd | 713 | */ |
fead3909 | 714 | void tipc_nametbl_subscribe(struct tipc_subscription *s) |
b97bf3fd PL |
715 | { |
716 | u32 type = s->seq.type; | |
993bfe5d | 717 | int index = hash(type); |
b97bf3fd PL |
718 | struct name_seq *seq; |
719 | ||
c4307285 | 720 | write_lock_bh(&tipc_nametbl_lock); |
b97bf3fd | 721 | seq = nametbl_find_seq(type); |
a016892c | 722 | if (!seq) |
993bfe5d YX |
723 | seq = tipc_nameseq_create(type, |
724 | &tipc_nametbl->seq_hlist[index]); | |
0e65967e | 725 | if (seq) { |
c4307285 | 726 | spin_lock_bh(&seq->lock); |
c4307285 YH |
727 | tipc_nameseq_subscribe(seq, s); |
728 | spin_unlock_bh(&seq->lock); | |
729 | } else { | |
2cf8aa19 EH |
730 | pr_warn("Failed to create subscription for {%u,%u,%u}\n", |
731 | s->seq.type, s->seq.lower, s->seq.upper); | |
c4307285 YH |
732 | } |
733 | write_unlock_bh(&tipc_nametbl_lock); | |
b97bf3fd PL |
734 | } |
735 | ||
736 | /** | |
4323add6 | 737 | * tipc_nametbl_unsubscribe - remove a subscription object from name table |
b97bf3fd | 738 | */ |
fead3909 | 739 | void tipc_nametbl_unsubscribe(struct tipc_subscription *s) |
b97bf3fd PL |
740 | { |
741 | struct name_seq *seq; | |
742 | ||
c4307285 YH |
743 | write_lock_bh(&tipc_nametbl_lock); |
744 | seq = nametbl_find_seq(s->seq.type); | |
0e65967e | 745 | if (seq != NULL) { |
c4307285 YH |
746 | spin_lock_bh(&seq->lock); |
747 | list_del_init(&s->nameseq_list); | |
fb9962f3 YX |
748 | if (!seq->first_free && list_empty(&seq->subscriptions)) { |
749 | hlist_del_init(&seq->ns_list); | |
750 | spin_unlock_bh(&seq->lock); | |
751 | kfree(seq->sseqs); | |
752 | kfree(seq); | |
753 | } else { | |
754 | spin_unlock_bh(&seq->lock); | |
755 | } | |
c4307285 YH |
756 | } |
757 | write_unlock_bh(&tipc_nametbl_lock); | |
b97bf3fd PL |
758 | } |
759 | ||
760 | ||
761 | /** | |
2c53040f | 762 | * subseq_list - print specified sub-sequence contents into the given buffer |
b97bf3fd | 763 | */ |
dc1aed37 | 764 | static int subseq_list(struct sub_seq *sseq, char *buf, int len, u32 depth, |
ae8509c4 | 765 | u32 index) |
b97bf3fd PL |
766 | { |
767 | char portIdStr[27]; | |
c2de5814 | 768 | const char *scope_str[] = {"", " zone", " cluster", " node"}; |
f6f0a4d2 AS |
769 | struct publication *publ; |
770 | struct name_info *info; | |
dc1aed37 | 771 | int ret; |
b97bf3fd | 772 | |
dc1aed37 | 773 | ret = tipc_snprintf(buf, len, "%-10u %-10u ", sseq->lower, sseq->upper); |
b97bf3fd | 774 | |
f6f0a4d2 | 775 | if (depth == 2) { |
dc1aed37 EH |
776 | ret += tipc_snprintf(buf - ret, len + ret, "\n"); |
777 | return ret; | |
b97bf3fd PL |
778 | } |
779 | ||
f6f0a4d2 AS |
780 | info = sseq->info; |
781 | ||
782 | list_for_each_entry(publ, &info->zone_list, zone_list) { | |
0e65967e | 783 | sprintf(portIdStr, "<%u.%u.%u:%u>", |
b97bf3fd PL |
784 | tipc_zone(publ->node), tipc_cluster(publ->node), |
785 | tipc_node(publ->node), publ->ref); | |
dc1aed37 | 786 | ret += tipc_snprintf(buf + ret, len - ret, "%-26s ", portIdStr); |
b97bf3fd | 787 | if (depth > 3) { |
dc1aed37 EH |
788 | ret += tipc_snprintf(buf + ret, len - ret, "%-10u %s", |
789 | publ->key, scope_str[publ->scope]); | |
b97bf3fd | 790 | } |
f6f0a4d2 | 791 | if (!list_is_last(&publ->zone_list, &info->zone_list)) |
dc1aed37 EH |
792 | ret += tipc_snprintf(buf + ret, len - ret, |
793 | "\n%33s", " "); | |
adccff34 | 794 | } |
b97bf3fd | 795 | |
dc1aed37 EH |
796 | ret += tipc_snprintf(buf + ret, len - ret, "\n"); |
797 | return ret; | |
b97bf3fd PL |
798 | } |
799 | ||
800 | /** | |
2c53040f | 801 | * nameseq_list - print specified name sequence contents into the given buffer |
b97bf3fd | 802 | */ |
dc1aed37 | 803 | static int nameseq_list(struct name_seq *seq, char *buf, int len, u32 depth, |
ae8509c4 | 804 | u32 type, u32 lowbound, u32 upbound, u32 index) |
b97bf3fd PL |
805 | { |
806 | struct sub_seq *sseq; | |
807 | char typearea[11]; | |
dc1aed37 | 808 | int ret = 0; |
b97bf3fd | 809 | |
0f15d364 | 810 | if (seq->first_free == 0) |
dc1aed37 | 811 | return 0; |
0f15d364 | 812 | |
b97bf3fd PL |
813 | sprintf(typearea, "%-10u", seq->type); |
814 | ||
815 | if (depth == 1) { | |
dc1aed37 EH |
816 | ret += tipc_snprintf(buf, len, "%s\n", typearea); |
817 | return ret; | |
b97bf3fd PL |
818 | } |
819 | ||
820 | for (sseq = seq->sseqs; sseq != &seq->sseqs[seq->first_free]; sseq++) { | |
821 | if ((lowbound <= sseq->upper) && (upbound >= sseq->lower)) { | |
dc1aed37 EH |
822 | ret += tipc_snprintf(buf + ret, len - ret, "%s ", |
823 | typearea); | |
307fdf5e | 824 | spin_lock_bh(&seq->lock); |
dc1aed37 EH |
825 | ret += subseq_list(sseq, buf + ret, len - ret, |
826 | depth, index); | |
307fdf5e | 827 | spin_unlock_bh(&seq->lock); |
b97bf3fd PL |
828 | sprintf(typearea, "%10s", " "); |
829 | } | |
830 | } | |
dc1aed37 | 831 | return ret; |
b97bf3fd PL |
832 | } |
833 | ||
834 | /** | |
835 | * nametbl_header - print name table header into the given buffer | |
836 | */ | |
dc1aed37 | 837 | static int nametbl_header(char *buf, int len, u32 depth) |
b97bf3fd | 838 | { |
c2de5814 AS |
839 | const char *header[] = { |
840 | "Type ", | |
841 | "Lower Upper ", | |
842 | "Port Identity ", | |
843 | "Publication Scope" | |
844 | }; | |
845 | ||
846 | int i; | |
dc1aed37 | 847 | int ret = 0; |
c2de5814 AS |
848 | |
849 | if (depth > 4) | |
850 | depth = 4; | |
851 | for (i = 0; i < depth; i++) | |
dc1aed37 EH |
852 | ret += tipc_snprintf(buf + ret, len - ret, header[i]); |
853 | ret += tipc_snprintf(buf + ret, len - ret, "\n"); | |
854 | return ret; | |
b97bf3fd PL |
855 | } |
856 | ||
857 | /** | |
858 | * nametbl_list - print specified name table contents into the given buffer | |
859 | */ | |
dc1aed37 | 860 | static int nametbl_list(char *buf, int len, u32 depth_info, |
ae8509c4 | 861 | u32 type, u32 lowbound, u32 upbound) |
b97bf3fd PL |
862 | { |
863 | struct hlist_head *seq_head; | |
b97bf3fd PL |
864 | struct name_seq *seq; |
865 | int all_types; | |
dc1aed37 | 866 | int ret = 0; |
b97bf3fd PL |
867 | u32 depth; |
868 | u32 i; | |
869 | ||
870 | all_types = (depth_info & TIPC_NTQ_ALLTYPES); | |
871 | depth = (depth_info & ~TIPC_NTQ_ALLTYPES); | |
872 | ||
873 | if (depth == 0) | |
dc1aed37 | 874 | return 0; |
b97bf3fd PL |
875 | |
876 | if (all_types) { | |
877 | /* display all entries in name table to specified depth */ | |
dc1aed37 | 878 | ret += nametbl_header(buf, len, depth); |
b97bf3fd PL |
879 | lowbound = 0; |
880 | upbound = ~0; | |
f046e7d9 | 881 | for (i = 0; i < TIPC_NAMETBL_SIZE; i++) { |
993bfe5d | 882 | seq_head = &tipc_nametbl->seq_hlist[i]; |
b67bfe0d | 883 | hlist_for_each_entry(seq, seq_head, ns_list) { |
dc1aed37 EH |
884 | ret += nameseq_list(seq, buf + ret, len - ret, |
885 | depth, seq->type, | |
886 | lowbound, upbound, i); | |
b97bf3fd PL |
887 | } |
888 | } | |
889 | } else { | |
890 | /* display only the sequence that matches the specified type */ | |
891 | if (upbound < lowbound) { | |
dc1aed37 EH |
892 | ret += tipc_snprintf(buf + ret, len - ret, |
893 | "invalid name sequence specified\n"); | |
894 | return ret; | |
b97bf3fd | 895 | } |
dc1aed37 | 896 | ret += nametbl_header(buf + ret, len - ret, depth); |
b97bf3fd | 897 | i = hash(type); |
993bfe5d | 898 | seq_head = &tipc_nametbl->seq_hlist[i]; |
b67bfe0d | 899 | hlist_for_each_entry(seq, seq_head, ns_list) { |
b97bf3fd | 900 | if (seq->type == type) { |
dc1aed37 EH |
901 | ret += nameseq_list(seq, buf + ret, len - ret, |
902 | depth, type, | |
903 | lowbound, upbound, i); | |
b97bf3fd PL |
904 | break; |
905 | } | |
906 | } | |
907 | } | |
dc1aed37 | 908 | return ret; |
b97bf3fd PL |
909 | } |
910 | ||
4323add6 | 911 | struct sk_buff *tipc_nametbl_get(const void *req_tlv_area, int req_tlv_space) |
b97bf3fd PL |
912 | { |
913 | struct sk_buff *buf; | |
914 | struct tipc_name_table_query *argv; | |
915 | struct tlv_desc *rep_tlv; | |
dc1aed37 EH |
916 | char *pb; |
917 | int pb_len; | |
b97bf3fd PL |
918 | int str_len; |
919 | ||
920 | if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NAME_TBL_QUERY)) | |
4323add6 | 921 | return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR); |
b97bf3fd | 922 | |
dc1aed37 | 923 | buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN)); |
b97bf3fd PL |
924 | if (!buf) |
925 | return NULL; | |
926 | ||
927 | rep_tlv = (struct tlv_desc *)buf->data; | |
dc1aed37 EH |
928 | pb = TLV_DATA(rep_tlv); |
929 | pb_len = ULTRA_STRING_MAX_LEN; | |
b97bf3fd | 930 | argv = (struct tipc_name_table_query *)TLV_DATA(req_tlv_area); |
4323add6 | 931 | read_lock_bh(&tipc_nametbl_lock); |
dc1aed37 EH |
932 | str_len = nametbl_list(pb, pb_len, ntohl(argv->depth), |
933 | ntohl(argv->type), | |
934 | ntohl(argv->lowbound), ntohl(argv->upbound)); | |
4323add6 | 935 | read_unlock_bh(&tipc_nametbl_lock); |
dc1aed37 | 936 | str_len += 1; /* for "\0" */ |
b97bf3fd PL |
937 | skb_put(buf, TLV_SPACE(str_len)); |
938 | TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len); | |
939 | ||
940 | return buf; | |
941 | } | |
942 | ||
4323add6 | 943 | int tipc_nametbl_init(void) |
b97bf3fd | 944 | { |
993bfe5d YX |
945 | int i; |
946 | ||
947 | tipc_nametbl = kzalloc(sizeof(*tipc_nametbl), GFP_ATOMIC); | |
948 | if (!tipc_nametbl) | |
b97bf3fd PL |
949 | return -ENOMEM; |
950 | ||
993bfe5d YX |
951 | for (i = 0; i < TIPC_NAMETBL_SIZE; i++) |
952 | INIT_HLIST_HEAD(&tipc_nametbl->seq_hlist[i]); | |
953 | ||
954 | INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_ZONE_SCOPE]); | |
955 | INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_CLUSTER_SCOPE]); | |
956 | INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_NODE_SCOPE]); | |
b97bf3fd PL |
957 | return 0; |
958 | } | |
959 | ||
1bb8dce5 EH |
960 | /** |
961 | * tipc_purge_publications - remove all publications for a given type | |
962 | * | |
963 | * tipc_nametbl_lock must be held when calling this function | |
964 | */ | |
965 | static void tipc_purge_publications(struct name_seq *seq) | |
966 | { | |
967 | struct publication *publ, *safe; | |
968 | struct sub_seq *sseq; | |
969 | struct name_info *info; | |
970 | ||
fb9962f3 | 971 | spin_lock_bh(&seq->lock); |
1bb8dce5 EH |
972 | sseq = seq->sseqs; |
973 | info = sseq->info; | |
974 | list_for_each_entry_safe(publ, safe, &info->zone_list, zone_list) { | |
975 | tipc_nametbl_remove_publ(publ->type, publ->lower, publ->node, | |
976 | publ->ref, publ->key); | |
1621b94d | 977 | kfree(publ); |
1bb8dce5 | 978 | } |
38622f41 | 979 | hlist_del_init(&seq->ns_list); |
fb9962f3 YX |
980 | spin_lock_bh(&seq->lock); |
981 | ||
38622f41 YX |
982 | kfree(seq->sseqs); |
983 | kfree(seq); | |
1bb8dce5 EH |
984 | } |
985 | ||
4323add6 | 986 | void tipc_nametbl_stop(void) |
b97bf3fd | 987 | { |
b97bf3fd | 988 | u32 i; |
1bb8dce5 EH |
989 | struct name_seq *seq; |
990 | struct hlist_head *seq_head; | |
991 | struct hlist_node *safe; | |
b97bf3fd | 992 | |
1bb8dce5 EH |
993 | /* Verify name table is empty and purge any lingering |
994 | * publications, then release the name table | |
995 | */ | |
4323add6 | 996 | write_lock_bh(&tipc_nametbl_lock); |
f046e7d9 | 997 | for (i = 0; i < TIPC_NAMETBL_SIZE; i++) { |
993bfe5d | 998 | if (hlist_empty(&tipc_nametbl->seq_hlist[i])) |
f705ab95 | 999 | continue; |
993bfe5d | 1000 | seq_head = &tipc_nametbl->seq_hlist[i]; |
1bb8dce5 EH |
1001 | hlist_for_each_entry_safe(seq, safe, seq_head, ns_list) { |
1002 | tipc_purge_publications(seq); | |
1003 | } | |
b97bf3fd | 1004 | } |
4323add6 | 1005 | write_unlock_bh(&tipc_nametbl_lock); |
993bfe5d YX |
1006 | |
1007 | kfree(tipc_nametbl); | |
1008 | ||
b97bf3fd | 1009 | } |
1593123a | 1010 | |
d8182804 RA |
1011 | static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg, |
1012 | struct name_seq *seq, | |
1013 | struct sub_seq *sseq, u32 *last_publ) | |
1593123a RA |
1014 | { |
1015 | void *hdr; | |
1016 | struct nlattr *attrs; | |
1017 | struct nlattr *publ; | |
1018 | struct publication *p; | |
1019 | ||
1020 | if (*last_publ) { | |
1021 | list_for_each_entry(p, &sseq->info->zone_list, zone_list) | |
1022 | if (p->key == *last_publ) | |
1023 | break; | |
1024 | if (p->key != *last_publ) | |
1025 | return -EPIPE; | |
1026 | } else { | |
1027 | p = list_first_entry(&sseq->info->zone_list, struct publication, | |
1028 | zone_list); | |
1029 | } | |
1030 | ||
1031 | list_for_each_entry_from(p, &sseq->info->zone_list, zone_list) { | |
1032 | *last_publ = p->key; | |
1033 | ||
1034 | hdr = genlmsg_put(msg->skb, msg->portid, msg->seq, | |
1035 | &tipc_genl_v2_family, NLM_F_MULTI, | |
1036 | TIPC_NL_NAME_TABLE_GET); | |
1037 | if (!hdr) | |
1038 | return -EMSGSIZE; | |
1039 | ||
1040 | attrs = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE); | |
1041 | if (!attrs) | |
1042 | goto msg_full; | |
1043 | ||
1044 | publ = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE_PUBL); | |
1045 | if (!publ) | |
1046 | goto attr_msg_full; | |
1047 | ||
1048 | if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, seq->type)) | |
1049 | goto publ_msg_full; | |
1050 | if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sseq->lower)) | |
1051 | goto publ_msg_full; | |
1052 | if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sseq->upper)) | |
1053 | goto publ_msg_full; | |
1054 | if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope)) | |
1055 | goto publ_msg_full; | |
1056 | if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->node)) | |
1057 | goto publ_msg_full; | |
1058 | if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->ref)) | |
1059 | goto publ_msg_full; | |
1060 | if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key)) | |
1061 | goto publ_msg_full; | |
1062 | ||
1063 | nla_nest_end(msg->skb, publ); | |
1064 | nla_nest_end(msg->skb, attrs); | |
1065 | genlmsg_end(msg->skb, hdr); | |
1066 | } | |
1067 | *last_publ = 0; | |
1068 | ||
1069 | return 0; | |
1070 | ||
1071 | publ_msg_full: | |
1072 | nla_nest_cancel(msg->skb, publ); | |
1073 | attr_msg_full: | |
1074 | nla_nest_cancel(msg->skb, attrs); | |
1075 | msg_full: | |
1076 | genlmsg_cancel(msg->skb, hdr); | |
1077 | ||
1078 | return -EMSGSIZE; | |
1079 | } | |
1080 | ||
d8182804 RA |
1081 | static int __tipc_nl_subseq_list(struct tipc_nl_msg *msg, struct name_seq *seq, |
1082 | u32 *last_lower, u32 *last_publ) | |
1593123a RA |
1083 | { |
1084 | struct sub_seq *sseq; | |
1085 | struct sub_seq *sseq_start; | |
1086 | int err; | |
1087 | ||
1088 | if (*last_lower) { | |
1089 | sseq_start = nameseq_find_subseq(seq, *last_lower); | |
1090 | if (!sseq_start) | |
1091 | return -EPIPE; | |
1092 | } else { | |
1093 | sseq_start = seq->sseqs; | |
1094 | } | |
1095 | ||
1096 | for (sseq = sseq_start; sseq != &seq->sseqs[seq->first_free]; sseq++) { | |
1097 | err = __tipc_nl_add_nametable_publ(msg, seq, sseq, last_publ); | |
1098 | if (err) { | |
1099 | *last_lower = sseq->lower; | |
1100 | return err; | |
1101 | } | |
1102 | } | |
1103 | *last_lower = 0; | |
1104 | ||
1105 | return 0; | |
1106 | } | |
1107 | ||
d8182804 RA |
1108 | static int __tipc_nl_seq_list(struct tipc_nl_msg *msg, u32 *last_type, |
1109 | u32 *last_lower, u32 *last_publ) | |
1593123a RA |
1110 | { |
1111 | struct hlist_head *seq_head; | |
1112 | struct name_seq *seq; | |
1113 | int err; | |
1114 | int i; | |
1115 | ||
1116 | if (*last_type) | |
1117 | i = hash(*last_type); | |
1118 | else | |
1119 | i = 0; | |
1120 | ||
1121 | for (; i < TIPC_NAMETBL_SIZE; i++) { | |
993bfe5d | 1122 | seq_head = &tipc_nametbl->seq_hlist[i]; |
1593123a RA |
1123 | |
1124 | if (*last_type) { | |
1125 | seq = nametbl_find_seq(*last_type); | |
1126 | if (!seq) | |
1127 | return -EPIPE; | |
1128 | } else { | |
1129 | seq = hlist_entry_safe((seq_head)->first, | |
1130 | struct name_seq, ns_list); | |
1131 | if (!seq) | |
1132 | continue; | |
1133 | } | |
1134 | ||
1135 | hlist_for_each_entry_from(seq, ns_list) { | |
1136 | spin_lock_bh(&seq->lock); | |
1593123a RA |
1137 | err = __tipc_nl_subseq_list(msg, seq, last_lower, |
1138 | last_publ); | |
1139 | ||
1140 | if (err) { | |
1141 | *last_type = seq->type; | |
1142 | spin_unlock_bh(&seq->lock); | |
1143 | return err; | |
1144 | } | |
1145 | spin_unlock_bh(&seq->lock); | |
1146 | } | |
1147 | *last_type = 0; | |
1148 | } | |
1149 | return 0; | |
1150 | } | |
1151 | ||
1152 | int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
1153 | { | |
1154 | int err; | |
1155 | int done = cb->args[3]; | |
1156 | u32 last_type = cb->args[0]; | |
1157 | u32 last_lower = cb->args[1]; | |
1158 | u32 last_publ = cb->args[2]; | |
1159 | struct tipc_nl_msg msg; | |
1160 | ||
1161 | if (done) | |
1162 | return 0; | |
1163 | ||
1164 | msg.skb = skb; | |
1165 | msg.portid = NETLINK_CB(cb->skb).portid; | |
1166 | msg.seq = cb->nlh->nlmsg_seq; | |
1167 | ||
1168 | read_lock_bh(&tipc_nametbl_lock); | |
1169 | ||
1170 | err = __tipc_nl_seq_list(&msg, &last_type, &last_lower, &last_publ); | |
1171 | if (!err) { | |
1172 | done = 1; | |
1173 | } else if (err != -EMSGSIZE) { | |
1174 | /* We never set seq or call nl_dump_check_consistent() this | |
1175 | * means that setting prev_seq here will cause the consistence | |
1176 | * check to fail in the netlink callback handler. Resulting in | |
1177 | * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if | |
1178 | * we got an error. | |
1179 | */ | |
1180 | cb->prev_seq = 1; | |
1181 | } | |
1182 | ||
1183 | read_unlock_bh(&tipc_nametbl_lock); | |
1184 | ||
1185 | cb->args[0] = last_type; | |
1186 | cb->args[1] = last_lower; | |
1187 | cb->args[2] = last_publ; | |
1188 | cb->args[3] = done; | |
1189 | ||
1190 | return skb->len; | |
1191 | } |