2 * net/tipc/name_table.c: TIPC name table code
4 * Copyright (c) 2000-2006, 2014, Ericsson AB
5 * Copyright (c) 2004-2008, 2010-2014, Wind River Systems
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
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.
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.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
39 #include "name_table.h"
40 #include "name_distr.h"
44 #define TIPC_NAMETBL_SIZE 1024 /* must be a power of 2 */
46 static const struct nla_policy
47 tipc_nl_name_table_policy
[TIPC_NLA_NAME_TABLE_MAX
+ 1] = {
48 [TIPC_NLA_NAME_TABLE_UNSPEC
] = { .type
= NLA_UNSPEC
},
49 [TIPC_NLA_NAME_TABLE_PUBL
] = { .type
= NLA_NESTED
}
53 * struct name_info - name sequence publication info
54 * @node_list: circular list of publications made by own node
55 * @cluster_list: circular list of publications made by own cluster
56 * @zone_list: circular list of publications made by own zone
57 * @node_list_size: number of entries in "node_list"
58 * @cluster_list_size: number of entries in "cluster_list"
59 * @zone_list_size: number of entries in "zone_list"
61 * Note: The zone list always contains at least one entry, since all
62 * publications of the associated name sequence belong to it.
63 * (The cluster and node lists may be empty.)
66 struct list_head node_list
;
67 struct list_head cluster_list
;
68 struct list_head zone_list
;
70 u32 cluster_list_size
;
75 * struct sub_seq - container for all published instances of a name sequence
76 * @lower: name sequence lower bound
77 * @upper: name sequence upper bound
78 * @info: pointer to name sequence publication info
83 struct name_info
*info
;
87 * struct name_seq - container for all published instances of a name type
88 * @type: 32 bit 'type' value for name sequence
89 * @sseq: pointer to dynamically-sized array of sub-sequences of this 'type';
90 * sub-sequences are sorted in ascending order
91 * @alloc: number of sub-sequences currently in array
92 * @first_free: array index of first unused sub-sequence entry
93 * @ns_list: links to adjacent name sequences in hash chain
94 * @subscriptions: list of subscriptions for this 'type'
95 * @lock: spinlock controlling access to publication lists of all sub-sequences
96 * @rcu: RCU callback head used for deferred freeing
100 struct sub_seq
*sseqs
;
103 struct hlist_node ns_list
;
104 struct list_head subscriptions
;
109 struct name_table
*tipc_nametbl
;
110 DEFINE_SPINLOCK(tipc_nametbl_lock
);
112 static int hash(int x
)
114 return x
& (TIPC_NAMETBL_SIZE
- 1);
118 * publ_create - create a publication structure
120 static struct publication
*publ_create(u32 type
, u32 lower
, u32 upper
,
121 u32 scope
, u32 node
, u32 port_ref
,
124 struct publication
*publ
= kzalloc(sizeof(*publ
), GFP_ATOMIC
);
126 pr_warn("Publication creation failure, no memory\n");
135 publ
->ref
= port_ref
;
137 INIT_LIST_HEAD(&publ
->pport_list
);
142 * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
144 static struct sub_seq
*tipc_subseq_alloc(u32 cnt
)
146 return kcalloc(cnt
, sizeof(struct sub_seq
), GFP_ATOMIC
);
150 * tipc_nameseq_create - create a name sequence structure for the specified 'type'
152 * Allocates a single sub-sequence structure and sets it to all 0's.
154 static struct name_seq
*tipc_nameseq_create(u32 type
, struct hlist_head
*seq_head
)
156 struct name_seq
*nseq
= kzalloc(sizeof(*nseq
), GFP_ATOMIC
);
157 struct sub_seq
*sseq
= tipc_subseq_alloc(1);
159 if (!nseq
|| !sseq
) {
160 pr_warn("Name sequence creation failed, no memory\n");
166 spin_lock_init(&nseq
->lock
);
170 INIT_HLIST_NODE(&nseq
->ns_list
);
171 INIT_LIST_HEAD(&nseq
->subscriptions
);
172 hlist_add_head_rcu(&nseq
->ns_list
, seq_head
);
177 * nameseq_find_subseq - find sub-sequence (if any) matching a name instance
179 * Very time-critical, so binary searches through sub-sequence array.
181 static struct sub_seq
*nameseq_find_subseq(struct name_seq
*nseq
,
184 struct sub_seq
*sseqs
= nseq
->sseqs
;
186 int high
= nseq
->first_free
- 1;
189 while (low
<= high
) {
190 mid
= (low
+ high
) / 2;
191 if (instance
< sseqs
[mid
].lower
)
193 else if (instance
> sseqs
[mid
].upper
)
202 * nameseq_locate_subseq - determine position of name instance in sub-sequence
204 * Returns index in sub-sequence array of the entry that contains the specified
205 * instance value; if no entry contains that value, returns the position
206 * where a new entry for it would be inserted in the array.
208 * Note: Similar to binary search code for locating a sub-sequence.
210 static u32
nameseq_locate_subseq(struct name_seq
*nseq
, u32 instance
)
212 struct sub_seq
*sseqs
= nseq
->sseqs
;
214 int high
= nseq
->first_free
- 1;
217 while (low
<= high
) {
218 mid
= (low
+ high
) / 2;
219 if (instance
< sseqs
[mid
].lower
)
221 else if (instance
> sseqs
[mid
].upper
)
230 * tipc_nameseq_insert_publ
232 static struct publication
*tipc_nameseq_insert_publ(struct name_seq
*nseq
,
233 u32 type
, u32 lower
, u32 upper
,
234 u32 scope
, u32 node
, u32 port
, u32 key
)
236 struct tipc_subscription
*s
;
237 struct tipc_subscription
*st
;
238 struct publication
*publ
;
239 struct sub_seq
*sseq
;
240 struct name_info
*info
;
241 int created_subseq
= 0;
243 sseq
= nameseq_find_subseq(nseq
, lower
);
246 /* Lower end overlaps existing entry => need an exact match */
247 if ((sseq
->lower
!= lower
) || (sseq
->upper
!= upper
)) {
253 /* Check if an identical publication already exists */
254 list_for_each_entry(publ
, &info
->zone_list
, zone_list
) {
255 if ((publ
->ref
== port
) && (publ
->key
== key
) &&
256 (!publ
->node
|| (publ
->node
== node
)))
261 struct sub_seq
*freesseq
;
263 /* Find where lower end should be inserted */
264 inspos
= nameseq_locate_subseq(nseq
, lower
);
266 /* Fail if upper end overlaps into an existing entry */
267 if ((inspos
< nseq
->first_free
) &&
268 (upper
>= nseq
->sseqs
[inspos
].lower
)) {
272 /* Ensure there is space for new sub-sequence */
273 if (nseq
->first_free
== nseq
->alloc
) {
274 struct sub_seq
*sseqs
= tipc_subseq_alloc(nseq
->alloc
* 2);
277 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
281 memcpy(sseqs
, nseq
->sseqs
,
282 nseq
->alloc
* sizeof(struct sub_seq
));
288 info
= kzalloc(sizeof(*info
), GFP_ATOMIC
);
290 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
295 INIT_LIST_HEAD(&info
->node_list
);
296 INIT_LIST_HEAD(&info
->cluster_list
);
297 INIT_LIST_HEAD(&info
->zone_list
);
299 /* Insert new sub-sequence */
300 sseq
= &nseq
->sseqs
[inspos
];
301 freesseq
= &nseq
->sseqs
[nseq
->first_free
];
302 memmove(sseq
+ 1, sseq
, (freesseq
- sseq
) * sizeof(*sseq
));
303 memset(sseq
, 0, sizeof(*sseq
));
311 /* Insert a publication */
312 publ
= publ_create(type
, lower
, upper
, scope
, node
, port
, key
);
316 list_add(&publ
->zone_list
, &info
->zone_list
);
317 info
->zone_list_size
++;
319 if (in_own_cluster(node
)) {
320 list_add(&publ
->cluster_list
, &info
->cluster_list
);
321 info
->cluster_list_size
++;
324 if (in_own_node(node
)) {
325 list_add(&publ
->node_list
, &info
->node_list
);
326 info
->node_list_size
++;
329 /* Any subscriptions waiting for notification? */
330 list_for_each_entry_safe(s
, st
, &nseq
->subscriptions
, nameseq_list
) {
331 tipc_subscr_report_overlap(s
,
343 * tipc_nameseq_remove_publ
345 * NOTE: There may be cases where TIPC is asked to remove a publication
346 * that is not in the name table. For example, if another node issues a
347 * publication for a name sequence that overlaps an existing name sequence
348 * the publication will not be recorded, which means the publication won't
349 * be found when the name sequence is later withdrawn by that node.
350 * A failed withdraw request simply returns a failure indication and lets the
351 * caller issue any error or warning messages associated with such a problem.
353 static struct publication
*tipc_nameseq_remove_publ(struct name_seq
*nseq
, u32 inst
,
354 u32 node
, u32 ref
, u32 key
)
356 struct publication
*publ
;
357 struct sub_seq
*sseq
= nameseq_find_subseq(nseq
, inst
);
358 struct name_info
*info
;
359 struct sub_seq
*free
;
360 struct tipc_subscription
*s
, *st
;
361 int removed_subseq
= 0;
368 /* Locate publication, if it exists */
369 list_for_each_entry(publ
, &info
->zone_list
, zone_list
) {
370 if ((publ
->key
== key
) && (publ
->ref
== ref
) &&
371 (!publ
->node
|| (publ
->node
== node
)))
377 /* Remove publication from zone scope list */
378 list_del(&publ
->zone_list
);
379 info
->zone_list_size
--;
381 /* Remove publication from cluster scope list, if present */
382 if (in_own_cluster(node
)) {
383 list_del(&publ
->cluster_list
);
384 info
->cluster_list_size
--;
387 /* Remove publication from node scope list, if present */
388 if (in_own_node(node
)) {
389 list_del(&publ
->node_list
);
390 info
->node_list_size
--;
393 /* Contract subseq list if no more publications for that subseq */
394 if (list_empty(&info
->zone_list
)) {
396 free
= &nseq
->sseqs
[nseq
->first_free
--];
397 memmove(sseq
, sseq
+ 1, (free
- (sseq
+ 1)) * sizeof(*sseq
));
401 /* Notify any waiting subscriptions */
402 list_for_each_entry_safe(s
, st
, &nseq
->subscriptions
, nameseq_list
) {
403 tipc_subscr_report_overlap(s
,
416 * tipc_nameseq_subscribe - attach a subscription, and issue
417 * the prescribed number of events if there is any sub-
418 * sequence overlapping with the requested sequence
420 static void tipc_nameseq_subscribe(struct name_seq
*nseq
,
421 struct tipc_subscription
*s
)
423 struct sub_seq
*sseq
= nseq
->sseqs
;
425 list_add(&s
->nameseq_list
, &nseq
->subscriptions
);
430 while (sseq
!= &nseq
->sseqs
[nseq
->first_free
]) {
431 if (tipc_subscr_overlap(s
, sseq
->lower
, sseq
->upper
)) {
432 struct publication
*crs
;
433 struct name_info
*info
= sseq
->info
;
436 list_for_each_entry(crs
, &info
->zone_list
, zone_list
) {
437 tipc_subscr_report_overlap(s
,
451 static struct name_seq
*nametbl_find_seq(u32 type
)
453 struct hlist_head
*seq_head
;
456 seq_head
= &tipc_nametbl
->seq_hlist
[hash(type
)];
457 hlist_for_each_entry_rcu(ns
, seq_head
, ns_list
) {
458 if (ns
->type
== type
)
465 struct publication
*tipc_nametbl_insert_publ(u32 type
, u32 lower
, u32 upper
,
466 u32 scope
, u32 node
, u32 port
, u32 key
)
468 struct publication
*publ
;
469 struct name_seq
*seq
= nametbl_find_seq(type
);
470 int index
= hash(type
);
472 if ((scope
< TIPC_ZONE_SCOPE
) || (scope
> TIPC_NODE_SCOPE
) ||
474 pr_debug("Failed to publish illegal {%u,%u,%u} with scope %u\n",
475 type
, lower
, upper
, scope
);
480 seq
= tipc_nameseq_create(type
,
481 &tipc_nametbl
->seq_hlist
[index
]);
485 spin_lock_bh(&seq
->lock
);
486 publ
= tipc_nameseq_insert_publ(seq
, type
, lower
, upper
,
487 scope
, node
, port
, key
);
488 spin_unlock_bh(&seq
->lock
);
492 struct publication
*tipc_nametbl_remove_publ(u32 type
, u32 lower
,
493 u32 node
, u32 ref
, u32 key
)
495 struct publication
*publ
;
496 struct name_seq
*seq
= nametbl_find_seq(type
);
501 spin_lock_bh(&seq
->lock
);
502 publ
= tipc_nameseq_remove_publ(seq
, lower
, node
, ref
, key
);
503 if (!seq
->first_free
&& list_empty(&seq
->subscriptions
)) {
504 hlist_del_init_rcu(&seq
->ns_list
);
506 spin_unlock_bh(&seq
->lock
);
510 spin_unlock_bh(&seq
->lock
);
515 * tipc_nametbl_translate - perform name translation
517 * On entry, 'destnode' is the search domain used during translation.
520 * - if name translation is deferred to another node/cluster/zone,
521 * leaves 'destnode' unchanged (will be non-zero) and returns 0
522 * - if name translation is attempted and succeeds, sets 'destnode'
523 * to publishing node and returns port reference (will be non-zero)
524 * - if name translation is attempted and fails, sets 'destnode' to 0
527 u32
tipc_nametbl_translate(u32 type
, u32 instance
, u32
*destnode
)
529 struct sub_seq
*sseq
;
530 struct name_info
*info
;
531 struct publication
*publ
;
532 struct name_seq
*seq
;
536 if (!tipc_in_scope(*destnode
, tipc_own_addr
))
540 seq
= nametbl_find_seq(type
);
543 spin_lock_bh(&seq
->lock
);
544 sseq
= nameseq_find_subseq(seq
, instance
);
549 /* Closest-First Algorithm */
550 if (likely(!*destnode
)) {
551 if (!list_empty(&info
->node_list
)) {
552 publ
= list_first_entry(&info
->node_list
,
555 list_move_tail(&publ
->node_list
,
557 } else if (!list_empty(&info
->cluster_list
)) {
558 publ
= list_first_entry(&info
->cluster_list
,
561 list_move_tail(&publ
->cluster_list
,
562 &info
->cluster_list
);
564 publ
= list_first_entry(&info
->zone_list
,
567 list_move_tail(&publ
->zone_list
,
572 /* Round-Robin Algorithm */
573 else if (*destnode
== tipc_own_addr
) {
574 if (list_empty(&info
->node_list
))
576 publ
= list_first_entry(&info
->node_list
, struct publication
,
578 list_move_tail(&publ
->node_list
, &info
->node_list
);
579 } else if (in_own_cluster_exact(*destnode
)) {
580 if (list_empty(&info
->cluster_list
))
582 publ
= list_first_entry(&info
->cluster_list
, struct publication
,
584 list_move_tail(&publ
->cluster_list
, &info
->cluster_list
);
586 publ
= list_first_entry(&info
->zone_list
, struct publication
,
588 list_move_tail(&publ
->zone_list
, &info
->zone_list
);
594 spin_unlock_bh(&seq
->lock
);
602 * tipc_nametbl_mc_translate - find multicast destinations
604 * Creates list of all local ports that overlap the given multicast address;
605 * also determines if any off-node ports overlap.
607 * Note: Publications with a scope narrower than 'limit' are ignored.
608 * (i.e. local node-scope publications mustn't receive messages arriving
609 * from another node, even if the multcast link brought it here)
611 * Returns non-zero if any off-node ports overlap
613 int tipc_nametbl_mc_translate(u32 type
, u32 lower
, u32 upper
, u32 limit
,
614 struct tipc_port_list
*dports
)
616 struct name_seq
*seq
;
617 struct sub_seq
*sseq
;
618 struct sub_seq
*sseq_stop
;
619 struct name_info
*info
;
623 seq
= nametbl_find_seq(type
);
627 spin_lock_bh(&seq
->lock
);
628 sseq
= seq
->sseqs
+ nameseq_locate_subseq(seq
, lower
);
629 sseq_stop
= seq
->sseqs
+ seq
->first_free
;
630 for (; sseq
!= sseq_stop
; sseq
++) {
631 struct publication
*publ
;
633 if (sseq
->lower
> upper
)
637 list_for_each_entry(publ
, &info
->node_list
, node_list
) {
638 if (publ
->scope
<= limit
)
639 tipc_port_list_add(dports
, publ
->ref
);
642 if (info
->cluster_list_size
!= info
->node_list_size
)
645 spin_unlock_bh(&seq
->lock
);
652 * tipc_nametbl_publish - add name publication to network name tables
654 struct publication
*tipc_nametbl_publish(struct net
*net
, u32 type
, u32 lower
,
655 u32 upper
, u32 scope
, u32 port_ref
,
658 struct publication
*publ
;
659 struct sk_buff
*buf
= NULL
;
661 spin_lock_bh(&tipc_nametbl_lock
);
662 if (tipc_nametbl
->local_publ_count
>= TIPC_MAX_PUBLICATIONS
) {
663 pr_warn("Publication failed, local publication limit reached (%u)\n",
664 TIPC_MAX_PUBLICATIONS
);
665 spin_unlock_bh(&tipc_nametbl_lock
);
669 publ
= tipc_nametbl_insert_publ(type
, lower
, upper
, scope
,
670 tipc_own_addr
, port_ref
, key
);
672 tipc_nametbl
->local_publ_count
++;
673 buf
= tipc_named_publish(publ
);
674 /* Any pending external events? */
675 tipc_named_process_backlog(net
);
677 spin_unlock_bh(&tipc_nametbl_lock
);
680 named_cluster_distribute(net
, buf
);
685 * tipc_nametbl_withdraw - withdraw name publication from network name tables
687 int tipc_nametbl_withdraw(struct net
*net
, u32 type
, u32 lower
, u32 ref
,
690 struct publication
*publ
;
691 struct sk_buff
*skb
= NULL
;
693 spin_lock_bh(&tipc_nametbl_lock
);
694 publ
= tipc_nametbl_remove_publ(type
, lower
, tipc_own_addr
, ref
, key
);
696 tipc_nametbl
->local_publ_count
--;
697 skb
= tipc_named_withdraw(publ
);
698 /* Any pending external events? */
699 tipc_named_process_backlog(net
);
700 list_del_init(&publ
->pport_list
);
701 kfree_rcu(publ
, rcu
);
703 pr_err("Unable to remove local publication\n"
704 "(type=%u, lower=%u, ref=%u, key=%u)\n",
705 type
, lower
, ref
, key
);
707 spin_unlock_bh(&tipc_nametbl_lock
);
710 named_cluster_distribute(net
, skb
);
717 * tipc_nametbl_subscribe - add a subscription object to the name table
719 void tipc_nametbl_subscribe(struct tipc_subscription
*s
)
721 u32 type
= s
->seq
.type
;
722 int index
= hash(type
);
723 struct name_seq
*seq
;
725 spin_lock_bh(&tipc_nametbl_lock
);
726 seq
= nametbl_find_seq(type
);
728 seq
= tipc_nameseq_create(type
,
729 &tipc_nametbl
->seq_hlist
[index
]);
731 spin_lock_bh(&seq
->lock
);
732 tipc_nameseq_subscribe(seq
, s
);
733 spin_unlock_bh(&seq
->lock
);
735 pr_warn("Failed to create subscription for {%u,%u,%u}\n",
736 s
->seq
.type
, s
->seq
.lower
, s
->seq
.upper
);
738 spin_unlock_bh(&tipc_nametbl_lock
);
742 * tipc_nametbl_unsubscribe - remove a subscription object from name table
744 void tipc_nametbl_unsubscribe(struct tipc_subscription
*s
)
746 struct name_seq
*seq
;
748 spin_lock_bh(&tipc_nametbl_lock
);
749 seq
= nametbl_find_seq(s
->seq
.type
);
751 spin_lock_bh(&seq
->lock
);
752 list_del_init(&s
->nameseq_list
);
753 if (!seq
->first_free
&& list_empty(&seq
->subscriptions
)) {
754 hlist_del_init_rcu(&seq
->ns_list
);
756 spin_unlock_bh(&seq
->lock
);
759 spin_unlock_bh(&seq
->lock
);
762 spin_unlock_bh(&tipc_nametbl_lock
);
766 * subseq_list - print specified sub-sequence contents into the given buffer
768 static int subseq_list(struct sub_seq
*sseq
, char *buf
, int len
, u32 depth
,
772 const char *scope_str
[] = {"", " zone", " cluster", " node"};
773 struct publication
*publ
;
774 struct name_info
*info
;
777 ret
= tipc_snprintf(buf
, len
, "%-10u %-10u ", sseq
->lower
, sseq
->upper
);
780 ret
+= tipc_snprintf(buf
- ret
, len
+ ret
, "\n");
786 list_for_each_entry(publ
, &info
->zone_list
, zone_list
) {
787 sprintf(portIdStr
, "<%u.%u.%u:%u>",
788 tipc_zone(publ
->node
), tipc_cluster(publ
->node
),
789 tipc_node(publ
->node
), publ
->ref
);
790 ret
+= tipc_snprintf(buf
+ ret
, len
- ret
, "%-26s ", portIdStr
);
792 ret
+= tipc_snprintf(buf
+ ret
, len
- ret
, "%-10u %s",
793 publ
->key
, scope_str
[publ
->scope
]);
795 if (!list_is_last(&publ
->zone_list
, &info
->zone_list
))
796 ret
+= tipc_snprintf(buf
+ ret
, len
- ret
,
800 ret
+= tipc_snprintf(buf
+ ret
, len
- ret
, "\n");
805 * nameseq_list - print specified name sequence contents into the given buffer
807 static int nameseq_list(struct name_seq
*seq
, char *buf
, int len
, u32 depth
,
808 u32 type
, u32 lowbound
, u32 upbound
, u32 index
)
810 struct sub_seq
*sseq
;
814 if (seq
->first_free
== 0)
817 sprintf(typearea
, "%-10u", seq
->type
);
820 ret
+= tipc_snprintf(buf
, len
, "%s\n", typearea
);
824 for (sseq
= seq
->sseqs
; sseq
!= &seq
->sseqs
[seq
->first_free
]; sseq
++) {
825 if ((lowbound
<= sseq
->upper
) && (upbound
>= sseq
->lower
)) {
826 ret
+= tipc_snprintf(buf
+ ret
, len
- ret
, "%s ",
828 spin_lock_bh(&seq
->lock
);
829 ret
+= subseq_list(sseq
, buf
+ ret
, len
- ret
,
831 spin_unlock_bh(&seq
->lock
);
832 sprintf(typearea
, "%10s", " ");
839 * nametbl_header - print name table header into the given buffer
841 static int nametbl_header(char *buf
, int len
, u32 depth
)
843 const char *header
[] = {
855 for (i
= 0; i
< depth
; i
++)
856 ret
+= tipc_snprintf(buf
+ ret
, len
- ret
, header
[i
]);
857 ret
+= tipc_snprintf(buf
+ ret
, len
- ret
, "\n");
862 * nametbl_list - print specified name table contents into the given buffer
864 static int nametbl_list(char *buf
, int len
, u32 depth_info
,
865 u32 type
, u32 lowbound
, u32 upbound
)
867 struct hlist_head
*seq_head
;
868 struct name_seq
*seq
;
874 all_types
= (depth_info
& TIPC_NTQ_ALLTYPES
);
875 depth
= (depth_info
& ~TIPC_NTQ_ALLTYPES
);
881 /* display all entries in name table to specified depth */
882 ret
+= nametbl_header(buf
, len
, depth
);
885 for (i
= 0; i
< TIPC_NAMETBL_SIZE
; i
++) {
886 seq_head
= &tipc_nametbl
->seq_hlist
[i
];
887 hlist_for_each_entry_rcu(seq
, seq_head
, ns_list
) {
888 ret
+= nameseq_list(seq
, buf
+ ret
, len
- ret
,
890 lowbound
, upbound
, i
);
894 /* display only the sequence that matches the specified type */
895 if (upbound
< lowbound
) {
896 ret
+= tipc_snprintf(buf
+ ret
, len
- ret
,
897 "invalid name sequence specified\n");
900 ret
+= nametbl_header(buf
+ ret
, len
- ret
, depth
);
902 seq_head
= &tipc_nametbl
->seq_hlist
[i
];
903 hlist_for_each_entry_rcu(seq
, seq_head
, ns_list
) {
904 if (seq
->type
== type
) {
905 ret
+= nameseq_list(seq
, buf
+ ret
, len
- ret
,
907 lowbound
, upbound
, i
);
915 struct sk_buff
*tipc_nametbl_get(const void *req_tlv_area
, int req_tlv_space
)
918 struct tipc_name_table_query
*argv
;
919 struct tlv_desc
*rep_tlv
;
924 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_NAME_TBL_QUERY
))
925 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
927 buf
= tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN
));
931 rep_tlv
= (struct tlv_desc
*)buf
->data
;
932 pb
= TLV_DATA(rep_tlv
);
933 pb_len
= ULTRA_STRING_MAX_LEN
;
934 argv
= (struct tipc_name_table_query
*)TLV_DATA(req_tlv_area
);
936 str_len
= nametbl_list(pb
, pb_len
, ntohl(argv
->depth
),
938 ntohl(argv
->lowbound
), ntohl(argv
->upbound
));
940 str_len
+= 1; /* for "\0" */
941 skb_put(buf
, TLV_SPACE(str_len
));
942 TLV_SET(rep_tlv
, TIPC_TLV_ULTRA_STRING
, NULL
, str_len
);
947 int tipc_nametbl_init(void)
951 tipc_nametbl
= kzalloc(sizeof(*tipc_nametbl
), GFP_ATOMIC
);
955 for (i
= 0; i
< TIPC_NAMETBL_SIZE
; i
++)
956 INIT_HLIST_HEAD(&tipc_nametbl
->seq_hlist
[i
]);
958 INIT_LIST_HEAD(&tipc_nametbl
->publ_list
[TIPC_ZONE_SCOPE
]);
959 INIT_LIST_HEAD(&tipc_nametbl
->publ_list
[TIPC_CLUSTER_SCOPE
]);
960 INIT_LIST_HEAD(&tipc_nametbl
->publ_list
[TIPC_NODE_SCOPE
]);
965 * tipc_purge_publications - remove all publications for a given type
967 * tipc_nametbl_lock must be held when calling this function
969 static void tipc_purge_publications(struct name_seq
*seq
)
971 struct publication
*publ
, *safe
;
972 struct sub_seq
*sseq
;
973 struct name_info
*info
;
975 spin_lock_bh(&seq
->lock
);
978 list_for_each_entry_safe(publ
, safe
, &info
->zone_list
, zone_list
) {
979 tipc_nametbl_remove_publ(publ
->type
, publ
->lower
, publ
->node
,
980 publ
->ref
, publ
->key
);
981 kfree_rcu(publ
, rcu
);
983 hlist_del_init_rcu(&seq
->ns_list
);
985 spin_unlock_bh(&seq
->lock
);
990 void tipc_nametbl_stop(void)
993 struct name_seq
*seq
;
994 struct hlist_head
*seq_head
;
996 /* Verify name table is empty and purge any lingering
997 * publications, then release the name table
999 spin_lock_bh(&tipc_nametbl_lock
);
1000 for (i
= 0; i
< TIPC_NAMETBL_SIZE
; i
++) {
1001 if (hlist_empty(&tipc_nametbl
->seq_hlist
[i
]))
1003 seq_head
= &tipc_nametbl
->seq_hlist
[i
];
1004 hlist_for_each_entry_rcu(seq
, seq_head
, ns_list
) {
1005 tipc_purge_publications(seq
);
1008 spin_unlock_bh(&tipc_nametbl_lock
);
1011 kfree(tipc_nametbl
);
1015 static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg
*msg
,
1016 struct name_seq
*seq
,
1017 struct sub_seq
*sseq
, u32
*last_publ
)
1020 struct nlattr
*attrs
;
1021 struct nlattr
*publ
;
1022 struct publication
*p
;
1025 list_for_each_entry(p
, &sseq
->info
->zone_list
, zone_list
)
1026 if (p
->key
== *last_publ
)
1028 if (p
->key
!= *last_publ
)
1031 p
= list_first_entry(&sseq
->info
->zone_list
, struct publication
,
1035 list_for_each_entry_from(p
, &sseq
->info
->zone_list
, zone_list
) {
1036 *last_publ
= p
->key
;
1038 hdr
= genlmsg_put(msg
->skb
, msg
->portid
, msg
->seq
,
1039 &tipc_genl_v2_family
, NLM_F_MULTI
,
1040 TIPC_NL_NAME_TABLE_GET
);
1044 attrs
= nla_nest_start(msg
->skb
, TIPC_NLA_NAME_TABLE
);
1048 publ
= nla_nest_start(msg
->skb
, TIPC_NLA_NAME_TABLE_PUBL
);
1052 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_TYPE
, seq
->type
))
1054 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_LOWER
, sseq
->lower
))
1056 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_UPPER
, sseq
->upper
))
1058 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_SCOPE
, p
->scope
))
1060 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_NODE
, p
->node
))
1062 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_REF
, p
->ref
))
1064 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_KEY
, p
->key
))
1067 nla_nest_end(msg
->skb
, publ
);
1068 nla_nest_end(msg
->skb
, attrs
);
1069 genlmsg_end(msg
->skb
, hdr
);
1076 nla_nest_cancel(msg
->skb
, publ
);
1078 nla_nest_cancel(msg
->skb
, attrs
);
1080 genlmsg_cancel(msg
->skb
, hdr
);
1085 static int __tipc_nl_subseq_list(struct tipc_nl_msg
*msg
, struct name_seq
*seq
,
1086 u32
*last_lower
, u32
*last_publ
)
1088 struct sub_seq
*sseq
;
1089 struct sub_seq
*sseq_start
;
1093 sseq_start
= nameseq_find_subseq(seq
, *last_lower
);
1097 sseq_start
= seq
->sseqs
;
1100 for (sseq
= sseq_start
; sseq
!= &seq
->sseqs
[seq
->first_free
]; sseq
++) {
1101 err
= __tipc_nl_add_nametable_publ(msg
, seq
, sseq
, last_publ
);
1103 *last_lower
= sseq
->lower
;
1112 static int __tipc_nl_seq_list(struct tipc_nl_msg
*msg
, u32
*last_type
,
1113 u32
*last_lower
, u32
*last_publ
)
1115 struct hlist_head
*seq_head
;
1116 struct name_seq
*seq
= NULL
;
1121 i
= hash(*last_type
);
1125 for (; i
< TIPC_NAMETBL_SIZE
; i
++) {
1126 seq_head
= &tipc_nametbl
->seq_hlist
[i
];
1129 seq
= nametbl_find_seq(*last_type
);
1133 hlist_for_each_entry_rcu(seq
, seq_head
, ns_list
)
1139 hlist_for_each_entry_from_rcu(seq
, ns_list
) {
1140 spin_lock_bh(&seq
->lock
);
1141 err
= __tipc_nl_subseq_list(msg
, seq
, last_lower
,
1145 *last_type
= seq
->type
;
1146 spin_unlock_bh(&seq
->lock
);
1149 spin_unlock_bh(&seq
->lock
);
1156 int tipc_nl_name_table_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1159 int done
= cb
->args
[3];
1160 u32 last_type
= cb
->args
[0];
1161 u32 last_lower
= cb
->args
[1];
1162 u32 last_publ
= cb
->args
[2];
1163 struct tipc_nl_msg msg
;
1169 msg
.portid
= NETLINK_CB(cb
->skb
).portid
;
1170 msg
.seq
= cb
->nlh
->nlmsg_seq
;
1173 err
= __tipc_nl_seq_list(&msg
, &last_type
, &last_lower
, &last_publ
);
1176 } else if (err
!= -EMSGSIZE
) {
1177 /* We never set seq or call nl_dump_check_consistent() this
1178 * means that setting prev_seq here will cause the consistence
1179 * check to fail in the netlink callback handler. Resulting in
1180 * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
1187 cb
->args
[0] = last_type
;
1188 cb
->args
[1] = last_lower
;
1189 cb
->args
[2] = last_publ
;