2 * net/tipc/name_table.c: TIPC name table code
4 * Copyright (c) 2000-2006, 2014-2015, 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.
40 #include "name_table.h"
41 #include "name_distr.h"
46 #include <net/genetlink.h>
48 #define TIPC_NAMETBL_SIZE 1024 /* must be a power of 2 */
50 static const struct nla_policy
51 tipc_nl_name_table_policy
[TIPC_NLA_NAME_TABLE_MAX
+ 1] = {
52 [TIPC_NLA_NAME_TABLE_UNSPEC
] = { .type
= NLA_UNSPEC
},
53 [TIPC_NLA_NAME_TABLE_PUBL
] = { .type
= NLA_NESTED
}
57 * struct name_info - name sequence publication info
58 * @node_list: circular list of publications made by own node
59 * @cluster_list: circular list of publications made by own cluster
60 * @zone_list: circular list of publications made by own zone
61 * @node_list_size: number of entries in "node_list"
62 * @cluster_list_size: number of entries in "cluster_list"
63 * @zone_list_size: number of entries in "zone_list"
65 * Note: The zone list always contains at least one entry, since all
66 * publications of the associated name sequence belong to it.
67 * (The cluster and node lists may be empty.)
70 struct list_head node_list
;
71 struct list_head cluster_list
;
72 struct list_head zone_list
;
74 u32 cluster_list_size
;
79 * struct sub_seq - container for all published instances of a name sequence
80 * @lower: name sequence lower bound
81 * @upper: name sequence upper bound
82 * @info: pointer to name sequence publication info
87 struct name_info
*info
;
91 * struct name_seq - container for all published instances of a name type
92 * @type: 32 bit 'type' value for name sequence
93 * @sseq: pointer to dynamically-sized array of sub-sequences of this 'type';
94 * sub-sequences are sorted in ascending order
95 * @alloc: number of sub-sequences currently in array
96 * @first_free: array index of first unused sub-sequence entry
97 * @ns_list: links to adjacent name sequences in hash chain
98 * @subscriptions: list of subscriptions for this 'type'
99 * @lock: spinlock controlling access to publication lists of all sub-sequences
100 * @rcu: RCU callback head used for deferred freeing
104 struct sub_seq
*sseqs
;
107 struct hlist_node ns_list
;
108 struct list_head subscriptions
;
113 static int hash(int x
)
115 return x
& (TIPC_NAMETBL_SIZE
- 1);
119 * publ_create - create a publication structure
121 static struct publication
*publ_create(u32 type
, u32 lower
, u32 upper
,
122 u32 scope
, u32 node
, u32 port_ref
,
125 struct publication
*publ
= kzalloc(sizeof(*publ
), GFP_ATOMIC
);
127 pr_warn("Publication creation failure, no memory\n");
136 publ
->ref
= port_ref
;
138 INIT_LIST_HEAD(&publ
->pport_list
);
143 * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
145 static struct sub_seq
*tipc_subseq_alloc(u32 cnt
)
147 return kcalloc(cnt
, sizeof(struct sub_seq
), GFP_ATOMIC
);
151 * tipc_nameseq_create - create a name sequence structure for the specified 'type'
153 * Allocates a single sub-sequence structure and sets it to all 0's.
155 static struct name_seq
*tipc_nameseq_create(u32 type
, struct hlist_head
*seq_head
)
157 struct name_seq
*nseq
= kzalloc(sizeof(*nseq
), GFP_ATOMIC
);
158 struct sub_seq
*sseq
= tipc_subseq_alloc(1);
160 if (!nseq
|| !sseq
) {
161 pr_warn("Name sequence creation failed, no memory\n");
167 spin_lock_init(&nseq
->lock
);
171 INIT_HLIST_NODE(&nseq
->ns_list
);
172 INIT_LIST_HEAD(&nseq
->subscriptions
);
173 hlist_add_head_rcu(&nseq
->ns_list
, seq_head
);
178 * nameseq_find_subseq - find sub-sequence (if any) matching a name instance
180 * Very time-critical, so binary searches through sub-sequence array.
182 static struct sub_seq
*nameseq_find_subseq(struct name_seq
*nseq
,
185 struct sub_seq
*sseqs
= nseq
->sseqs
;
187 int high
= nseq
->first_free
- 1;
190 while (low
<= high
) {
191 mid
= (low
+ high
) / 2;
192 if (instance
< sseqs
[mid
].lower
)
194 else if (instance
> sseqs
[mid
].upper
)
203 * nameseq_locate_subseq - determine position of name instance in sub-sequence
205 * Returns index in sub-sequence array of the entry that contains the specified
206 * instance value; if no entry contains that value, returns the position
207 * where a new entry for it would be inserted in the array.
209 * Note: Similar to binary search code for locating a sub-sequence.
211 static u32
nameseq_locate_subseq(struct name_seq
*nseq
, u32 instance
)
213 struct sub_seq
*sseqs
= nseq
->sseqs
;
215 int high
= nseq
->first_free
- 1;
218 while (low
<= high
) {
219 mid
= (low
+ high
) / 2;
220 if (instance
< sseqs
[mid
].lower
)
222 else if (instance
> sseqs
[mid
].upper
)
231 * tipc_nameseq_insert_publ
233 static struct publication
*tipc_nameseq_insert_publ(struct net
*net
,
234 struct name_seq
*nseq
,
236 u32 upper
, u32 scope
,
237 u32 node
, u32 port
, u32 key
)
239 struct tipc_subscription
*s
;
240 struct tipc_subscription
*st
;
241 struct publication
*publ
;
242 struct sub_seq
*sseq
;
243 struct name_info
*info
;
244 int created_subseq
= 0;
246 sseq
= nameseq_find_subseq(nseq
, lower
);
249 /* Lower end overlaps existing entry => need an exact match */
250 if ((sseq
->lower
!= lower
) || (sseq
->upper
!= upper
)) {
256 /* Check if an identical publication already exists */
257 list_for_each_entry(publ
, &info
->zone_list
, zone_list
) {
258 if ((publ
->ref
== port
) && (publ
->key
== key
) &&
259 (!publ
->node
|| (publ
->node
== node
)))
264 struct sub_seq
*freesseq
;
266 /* Find where lower end should be inserted */
267 inspos
= nameseq_locate_subseq(nseq
, lower
);
269 /* Fail if upper end overlaps into an existing entry */
270 if ((inspos
< nseq
->first_free
) &&
271 (upper
>= nseq
->sseqs
[inspos
].lower
)) {
275 /* Ensure there is space for new sub-sequence */
276 if (nseq
->first_free
== nseq
->alloc
) {
277 struct sub_seq
*sseqs
= tipc_subseq_alloc(nseq
->alloc
* 2);
280 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
284 memcpy(sseqs
, nseq
->sseqs
,
285 nseq
->alloc
* sizeof(struct sub_seq
));
291 info
= kzalloc(sizeof(*info
), GFP_ATOMIC
);
293 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
298 INIT_LIST_HEAD(&info
->node_list
);
299 INIT_LIST_HEAD(&info
->cluster_list
);
300 INIT_LIST_HEAD(&info
->zone_list
);
302 /* Insert new sub-sequence */
303 sseq
= &nseq
->sseqs
[inspos
];
304 freesseq
= &nseq
->sseqs
[nseq
->first_free
];
305 memmove(sseq
+ 1, sseq
, (freesseq
- sseq
) * sizeof(*sseq
));
306 memset(sseq
, 0, sizeof(*sseq
));
314 /* Insert a publication */
315 publ
= publ_create(type
, lower
, upper
, scope
, node
, port
, key
);
319 list_add(&publ
->zone_list
, &info
->zone_list
);
320 info
->zone_list_size
++;
322 if (in_own_cluster(net
, node
)) {
323 list_add(&publ
->cluster_list
, &info
->cluster_list
);
324 info
->cluster_list_size
++;
327 if (in_own_node(net
, node
)) {
328 list_add(&publ
->node_list
, &info
->node_list
);
329 info
->node_list_size
++;
332 /* Any subscriptions waiting for notification? */
333 list_for_each_entry_safe(s
, st
, &nseq
->subscriptions
, nameseq_list
) {
334 tipc_subscrp_report_overlap(s
, publ
->lower
, publ
->upper
,
335 TIPC_PUBLISHED
, publ
->ref
,
336 publ
->node
, created_subseq
);
342 * tipc_nameseq_remove_publ
344 * NOTE: There may be cases where TIPC is asked to remove a publication
345 * that is not in the name table. For example, if another node issues a
346 * publication for a name sequence that overlaps an existing name sequence
347 * the publication will not be recorded, which means the publication won't
348 * be found when the name sequence is later withdrawn by that node.
349 * A failed withdraw request simply returns a failure indication and lets the
350 * caller issue any error or warning messages associated with such a problem.
352 static struct publication
*tipc_nameseq_remove_publ(struct net
*net
,
353 struct name_seq
*nseq
,
357 struct publication
*publ
;
358 struct sub_seq
*sseq
= nameseq_find_subseq(nseq
, inst
);
359 struct name_info
*info
;
360 struct sub_seq
*free
;
361 struct tipc_subscription
*s
, *st
;
362 int removed_subseq
= 0;
369 /* Locate publication, if it exists */
370 list_for_each_entry(publ
, &info
->zone_list
, zone_list
) {
371 if ((publ
->key
== key
) && (publ
->ref
== ref
) &&
372 (!publ
->node
|| (publ
->node
== node
)))
378 /* Remove publication from zone scope list */
379 list_del(&publ
->zone_list
);
380 info
->zone_list_size
--;
382 /* Remove publication from cluster scope list, if present */
383 if (in_own_cluster(net
, node
)) {
384 list_del(&publ
->cluster_list
);
385 info
->cluster_list_size
--;
388 /* Remove publication from node scope list, if present */
389 if (in_own_node(net
, node
)) {
390 list_del(&publ
->node_list
);
391 info
->node_list_size
--;
394 /* Contract subseq list if no more publications for that subseq */
395 if (list_empty(&info
->zone_list
)) {
397 free
= &nseq
->sseqs
[nseq
->first_free
--];
398 memmove(sseq
, sseq
+ 1, (free
- (sseq
+ 1)) * sizeof(*sseq
));
402 /* Notify any waiting subscriptions */
403 list_for_each_entry_safe(s
, st
, &nseq
->subscriptions
, nameseq_list
) {
404 tipc_subscrp_report_overlap(s
, publ
->lower
, publ
->upper
,
405 TIPC_WITHDRAWN
, publ
->ref
,
406 publ
->node
, removed_subseq
);
413 * tipc_nameseq_subscribe - attach a subscription, and issue
414 * the prescribed number of events if there is any sub-
415 * sequence overlapping with the requested sequence
417 static void tipc_nameseq_subscribe(struct name_seq
*nseq
,
418 struct tipc_subscription
*s
)
420 struct sub_seq
*sseq
= nseq
->sseqs
;
421 struct tipc_name_seq ns
;
423 tipc_subscrp_convert_seq(&s
->evt
.s
.seq
, s
->swap
, &ns
);
425 list_add(&s
->nameseq_list
, &nseq
->subscriptions
);
430 while (sseq
!= &nseq
->sseqs
[nseq
->first_free
]) {
431 if (tipc_subscrp_check_overlap(&ns
, 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_subscrp_report_overlap(s
, sseq
->lower
,
449 static struct name_seq
*nametbl_find_seq(struct net
*net
, u32 type
)
451 struct tipc_net
*tn
= net_generic(net
, tipc_net_id
);
452 struct hlist_head
*seq_head
;
455 seq_head
= &tn
->nametbl
->seq_hlist
[hash(type
)];
456 hlist_for_each_entry_rcu(ns
, seq_head
, ns_list
) {
457 if (ns
->type
== type
)
464 struct publication
*tipc_nametbl_insert_publ(struct net
*net
, u32 type
,
465 u32 lower
, u32 upper
, u32 scope
,
466 u32 node
, u32 port
, u32 key
)
468 struct tipc_net
*tn
= net_generic(net
, tipc_net_id
);
469 struct publication
*publ
;
470 struct name_seq
*seq
= nametbl_find_seq(net
, type
);
471 int index
= hash(type
);
473 if ((scope
< TIPC_ZONE_SCOPE
) || (scope
> TIPC_NODE_SCOPE
) ||
475 pr_debug("Failed to publish illegal {%u,%u,%u} with scope %u\n",
476 type
, lower
, upper
, scope
);
481 seq
= tipc_nameseq_create(type
, &tn
->nametbl
->seq_hlist
[index
]);
485 spin_lock_bh(&seq
->lock
);
486 publ
= tipc_nameseq_insert_publ(net
, seq
, type
, lower
, upper
,
487 scope
, node
, port
, key
);
488 spin_unlock_bh(&seq
->lock
);
492 struct publication
*tipc_nametbl_remove_publ(struct net
*net
, u32 type
,
493 u32 lower
, u32 node
, u32 ref
,
496 struct publication
*publ
;
497 struct name_seq
*seq
= nametbl_find_seq(net
, type
);
502 spin_lock_bh(&seq
->lock
);
503 publ
= tipc_nameseq_remove_publ(net
, seq
, lower
, node
, ref
, key
);
504 if (!seq
->first_free
&& list_empty(&seq
->subscriptions
)) {
505 hlist_del_init_rcu(&seq
->ns_list
);
507 spin_unlock_bh(&seq
->lock
);
511 spin_unlock_bh(&seq
->lock
);
516 * tipc_nametbl_translate - perform name translation
518 * On entry, 'destnode' is the search domain used during translation.
521 * - if name translation is deferred to another node/cluster/zone,
522 * leaves 'destnode' unchanged (will be non-zero) and returns 0
523 * - if name translation is attempted and succeeds, sets 'destnode'
524 * to publishing node and returns port reference (will be non-zero)
525 * - if name translation is attempted and fails, sets 'destnode' to 0
528 u32
tipc_nametbl_translate(struct net
*net
, u32 type
, u32 instance
,
531 struct tipc_net
*tn
= net_generic(net
, tipc_net_id
);
532 struct sub_seq
*sseq
;
533 struct name_info
*info
;
534 struct publication
*publ
;
535 struct name_seq
*seq
;
539 if (!tipc_in_scope(*destnode
, tn
->own_addr
))
543 seq
= nametbl_find_seq(net
, type
);
546 spin_lock_bh(&seq
->lock
);
547 sseq
= nameseq_find_subseq(seq
, instance
);
552 /* Closest-First Algorithm */
553 if (likely(!*destnode
)) {
554 if (!list_empty(&info
->node_list
)) {
555 publ
= list_first_entry(&info
->node_list
,
558 list_move_tail(&publ
->node_list
,
560 } else if (!list_empty(&info
->cluster_list
)) {
561 publ
= list_first_entry(&info
->cluster_list
,
564 list_move_tail(&publ
->cluster_list
,
565 &info
->cluster_list
);
567 publ
= list_first_entry(&info
->zone_list
,
570 list_move_tail(&publ
->zone_list
,
575 /* Round-Robin Algorithm */
576 else if (*destnode
== tn
->own_addr
) {
577 if (list_empty(&info
->node_list
))
579 publ
= list_first_entry(&info
->node_list
, struct publication
,
581 list_move_tail(&publ
->node_list
, &info
->node_list
);
582 } else if (in_own_cluster_exact(net
, *destnode
)) {
583 if (list_empty(&info
->cluster_list
))
585 publ
= list_first_entry(&info
->cluster_list
, struct publication
,
587 list_move_tail(&publ
->cluster_list
, &info
->cluster_list
);
589 publ
= list_first_entry(&info
->zone_list
, struct publication
,
591 list_move_tail(&publ
->zone_list
, &info
->zone_list
);
597 spin_unlock_bh(&seq
->lock
);
605 * tipc_nametbl_mc_translate - find multicast destinations
607 * Creates list of all local ports that overlap the given multicast address;
608 * also determines if any off-node ports overlap.
610 * Note: Publications with a scope narrower than 'limit' are ignored.
611 * (i.e. local node-scope publications mustn't receive messages arriving
612 * from another node, even if the multcast link brought it here)
614 * Returns non-zero if any off-node ports overlap
616 int tipc_nametbl_mc_translate(struct net
*net
, u32 type
, u32 lower
, u32 upper
,
617 u32 limit
, struct tipc_plist
*dports
)
619 struct name_seq
*seq
;
620 struct sub_seq
*sseq
;
621 struct sub_seq
*sseq_stop
;
622 struct name_info
*info
;
626 seq
= nametbl_find_seq(net
, type
);
630 spin_lock_bh(&seq
->lock
);
631 sseq
= seq
->sseqs
+ nameseq_locate_subseq(seq
, lower
);
632 sseq_stop
= seq
->sseqs
+ seq
->first_free
;
633 for (; sseq
!= sseq_stop
; sseq
++) {
634 struct publication
*publ
;
636 if (sseq
->lower
> upper
)
640 list_for_each_entry(publ
, &info
->node_list
, node_list
) {
641 if (publ
->scope
<= limit
)
642 tipc_plist_push(dports
, publ
->ref
);
645 if (info
->cluster_list_size
!= info
->node_list_size
)
648 spin_unlock_bh(&seq
->lock
);
655 * tipc_nametbl_publish - add name publication to network name tables
657 struct publication
*tipc_nametbl_publish(struct net
*net
, u32 type
, u32 lower
,
658 u32 upper
, u32 scope
, u32 port_ref
,
661 struct publication
*publ
;
662 struct sk_buff
*buf
= NULL
;
663 struct tipc_net
*tn
= net_generic(net
, tipc_net_id
);
665 spin_lock_bh(&tn
->nametbl_lock
);
666 if (tn
->nametbl
->local_publ_count
>= TIPC_MAX_PUBLICATIONS
) {
667 pr_warn("Publication failed, local publication limit reached (%u)\n",
668 TIPC_MAX_PUBLICATIONS
);
669 spin_unlock_bh(&tn
->nametbl_lock
);
673 publ
= tipc_nametbl_insert_publ(net
, type
, lower
, upper
, scope
,
674 tn
->own_addr
, port_ref
, key
);
676 tn
->nametbl
->local_publ_count
++;
677 buf
= tipc_named_publish(net
, publ
);
678 /* Any pending external events? */
679 tipc_named_process_backlog(net
);
681 spin_unlock_bh(&tn
->nametbl_lock
);
684 tipc_node_broadcast(net
, buf
);
689 * tipc_nametbl_withdraw - withdraw name publication from network name tables
691 int tipc_nametbl_withdraw(struct net
*net
, u32 type
, u32 lower
, u32 ref
,
694 struct publication
*publ
;
695 struct sk_buff
*skb
= NULL
;
696 struct tipc_net
*tn
= net_generic(net
, tipc_net_id
);
698 spin_lock_bh(&tn
->nametbl_lock
);
699 publ
= tipc_nametbl_remove_publ(net
, type
, lower
, tn
->own_addr
,
702 tn
->nametbl
->local_publ_count
--;
703 skb
= tipc_named_withdraw(net
, publ
);
704 /* Any pending external events? */
705 tipc_named_process_backlog(net
);
706 list_del_init(&publ
->pport_list
);
707 kfree_rcu(publ
, rcu
);
709 pr_err("Unable to remove local publication\n"
710 "(type=%u, lower=%u, ref=%u, key=%u)\n",
711 type
, lower
, ref
, key
);
713 spin_unlock_bh(&tn
->nametbl_lock
);
716 tipc_node_broadcast(net
, skb
);
723 * tipc_nametbl_subscribe - add a subscription object to the name table
725 void tipc_nametbl_subscribe(struct tipc_subscription
*s
)
727 struct tipc_net
*tn
= net_generic(s
->net
, tipc_net_id
);
728 u32 type
= tipc_subscrp_convert_seq_type(s
->evt
.s
.seq
.type
, s
->swap
);
729 int index
= hash(type
);
730 struct name_seq
*seq
;
731 struct tipc_name_seq ns
;
733 spin_lock_bh(&tn
->nametbl_lock
);
734 seq
= nametbl_find_seq(s
->net
, type
);
736 seq
= tipc_nameseq_create(type
, &tn
->nametbl
->seq_hlist
[index
]);
738 spin_lock_bh(&seq
->lock
);
739 tipc_nameseq_subscribe(seq
, s
);
740 spin_unlock_bh(&seq
->lock
);
742 tipc_subscrp_convert_seq(&s
->evt
.s
.seq
, s
->swap
, &ns
);
743 pr_warn("Failed to create subscription for {%u,%u,%u}\n",
744 ns
.type
, ns
.lower
, ns
.upper
);
746 spin_unlock_bh(&tn
->nametbl_lock
);
750 * tipc_nametbl_unsubscribe - remove a subscription object from name table
752 void tipc_nametbl_unsubscribe(struct tipc_subscription
*s
)
754 struct tipc_net
*tn
= net_generic(s
->net
, tipc_net_id
);
755 struct name_seq
*seq
;
756 u32 type
= tipc_subscrp_convert_seq_type(s
->evt
.s
.seq
.type
, s
->swap
);
758 spin_lock_bh(&tn
->nametbl_lock
);
759 seq
= nametbl_find_seq(s
->net
, type
);
761 spin_lock_bh(&seq
->lock
);
762 list_del_init(&s
->nameseq_list
);
763 if (!seq
->first_free
&& list_empty(&seq
->subscriptions
)) {
764 hlist_del_init_rcu(&seq
->ns_list
);
766 spin_unlock_bh(&seq
->lock
);
769 spin_unlock_bh(&seq
->lock
);
772 spin_unlock_bh(&tn
->nametbl_lock
);
775 int tipc_nametbl_init(struct net
*net
)
777 struct tipc_net
*tn
= net_generic(net
, tipc_net_id
);
778 struct name_table
*tipc_nametbl
;
781 tipc_nametbl
= kzalloc(sizeof(*tipc_nametbl
), GFP_ATOMIC
);
785 for (i
= 0; i
< TIPC_NAMETBL_SIZE
; i
++)
786 INIT_HLIST_HEAD(&tipc_nametbl
->seq_hlist
[i
]);
788 INIT_LIST_HEAD(&tipc_nametbl
->publ_list
[TIPC_ZONE_SCOPE
]);
789 INIT_LIST_HEAD(&tipc_nametbl
->publ_list
[TIPC_CLUSTER_SCOPE
]);
790 INIT_LIST_HEAD(&tipc_nametbl
->publ_list
[TIPC_NODE_SCOPE
]);
791 tn
->nametbl
= tipc_nametbl
;
792 spin_lock_init(&tn
->nametbl_lock
);
797 * tipc_purge_publications - remove all publications for a given type
799 * tipc_nametbl_lock must be held when calling this function
801 static void tipc_purge_publications(struct net
*net
, struct name_seq
*seq
)
803 struct publication
*publ
, *safe
;
804 struct sub_seq
*sseq
;
805 struct name_info
*info
;
807 spin_lock_bh(&seq
->lock
);
810 list_for_each_entry_safe(publ
, safe
, &info
->zone_list
, zone_list
) {
811 tipc_nameseq_remove_publ(net
, seq
, publ
->lower
, publ
->node
,
812 publ
->ref
, publ
->key
);
813 kfree_rcu(publ
, rcu
);
815 hlist_del_init_rcu(&seq
->ns_list
);
817 spin_unlock_bh(&seq
->lock
);
822 void tipc_nametbl_stop(struct net
*net
)
825 struct name_seq
*seq
;
826 struct hlist_head
*seq_head
;
827 struct tipc_net
*tn
= net_generic(net
, tipc_net_id
);
828 struct name_table
*tipc_nametbl
= tn
->nametbl
;
830 /* Verify name table is empty and purge any lingering
831 * publications, then release the name table
833 spin_lock_bh(&tn
->nametbl_lock
);
834 for (i
= 0; i
< TIPC_NAMETBL_SIZE
; i
++) {
835 if (hlist_empty(&tipc_nametbl
->seq_hlist
[i
]))
837 seq_head
= &tipc_nametbl
->seq_hlist
[i
];
838 hlist_for_each_entry_rcu(seq
, seq_head
, ns_list
) {
839 tipc_purge_publications(net
, seq
);
842 spin_unlock_bh(&tn
->nametbl_lock
);
849 static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg
*msg
,
850 struct name_seq
*seq
,
851 struct sub_seq
*sseq
, u32
*last_publ
)
854 struct nlattr
*attrs
;
856 struct publication
*p
;
859 list_for_each_entry(p
, &sseq
->info
->zone_list
, zone_list
)
860 if (p
->key
== *last_publ
)
862 if (p
->key
!= *last_publ
)
865 p
= list_first_entry(&sseq
->info
->zone_list
, struct publication
,
869 list_for_each_entry_from(p
, &sseq
->info
->zone_list
, zone_list
) {
872 hdr
= genlmsg_put(msg
->skb
, msg
->portid
, msg
->seq
,
873 &tipc_genl_family
, NLM_F_MULTI
,
874 TIPC_NL_NAME_TABLE_GET
);
878 attrs
= nla_nest_start(msg
->skb
, TIPC_NLA_NAME_TABLE
);
882 publ
= nla_nest_start(msg
->skb
, TIPC_NLA_NAME_TABLE_PUBL
);
886 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_TYPE
, seq
->type
))
888 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_LOWER
, sseq
->lower
))
890 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_UPPER
, sseq
->upper
))
892 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_SCOPE
, p
->scope
))
894 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_NODE
, p
->node
))
896 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_REF
, p
->ref
))
898 if (nla_put_u32(msg
->skb
, TIPC_NLA_PUBL_KEY
, p
->key
))
901 nla_nest_end(msg
->skb
, publ
);
902 nla_nest_end(msg
->skb
, attrs
);
903 genlmsg_end(msg
->skb
, hdr
);
910 nla_nest_cancel(msg
->skb
, publ
);
912 nla_nest_cancel(msg
->skb
, attrs
);
914 genlmsg_cancel(msg
->skb
, hdr
);
919 static int __tipc_nl_subseq_list(struct tipc_nl_msg
*msg
, struct name_seq
*seq
,
920 u32
*last_lower
, u32
*last_publ
)
922 struct sub_seq
*sseq
;
923 struct sub_seq
*sseq_start
;
927 sseq_start
= nameseq_find_subseq(seq
, *last_lower
);
931 sseq_start
= seq
->sseqs
;
934 for (sseq
= sseq_start
; sseq
!= &seq
->sseqs
[seq
->first_free
]; sseq
++) {
935 err
= __tipc_nl_add_nametable_publ(msg
, seq
, sseq
, last_publ
);
937 *last_lower
= sseq
->lower
;
946 static int tipc_nl_seq_list(struct net
*net
, struct tipc_nl_msg
*msg
,
947 u32
*last_type
, u32
*last_lower
, u32
*last_publ
)
949 struct tipc_net
*tn
= net_generic(net
, tipc_net_id
);
950 struct hlist_head
*seq_head
;
951 struct name_seq
*seq
= NULL
;
956 i
= hash(*last_type
);
960 for (; i
< TIPC_NAMETBL_SIZE
; i
++) {
961 seq_head
= &tn
->nametbl
->seq_hlist
[i
];
964 seq
= nametbl_find_seq(net
, *last_type
);
968 hlist_for_each_entry_rcu(seq
, seq_head
, ns_list
)
974 hlist_for_each_entry_from_rcu(seq
, ns_list
) {
975 spin_lock_bh(&seq
->lock
);
976 err
= __tipc_nl_subseq_list(msg
, seq
, last_lower
,
980 *last_type
= seq
->type
;
981 spin_unlock_bh(&seq
->lock
);
984 spin_unlock_bh(&seq
->lock
);
991 int tipc_nl_name_table_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
994 int done
= cb
->args
[3];
995 u32 last_type
= cb
->args
[0];
996 u32 last_lower
= cb
->args
[1];
997 u32 last_publ
= cb
->args
[2];
998 struct net
*net
= sock_net(skb
->sk
);
999 struct tipc_nl_msg msg
;
1005 msg
.portid
= NETLINK_CB(cb
->skb
).portid
;
1006 msg
.seq
= cb
->nlh
->nlmsg_seq
;
1009 err
= tipc_nl_seq_list(net
, &msg
, &last_type
, &last_lower
, &last_publ
);
1012 } else if (err
!= -EMSGSIZE
) {
1013 /* We never set seq or call nl_dump_check_consistent() this
1014 * means that setting prev_seq here will cause the consistence
1015 * check to fail in the netlink callback handler. Resulting in
1016 * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
1023 cb
->args
[0] = last_type
;
1024 cb
->args
[1] = last_lower
;
1025 cb
->args
[2] = last_publ
;
1031 void tipc_plist_push(struct tipc_plist
*pl
, u32 port
)
1033 struct tipc_plist
*nl
;
1035 if (likely(!pl
->port
)) {
1039 if (pl
->port
== port
)
1041 list_for_each_entry(nl
, &pl
->list
, list
) {
1042 if (nl
->port
== port
)
1045 nl
= kmalloc(sizeof(*nl
), GFP_ATOMIC
);
1048 list_add(&nl
->list
, &pl
->list
);
1052 u32
tipc_plist_pop(struct tipc_plist
*pl
)
1054 struct tipc_plist
*nl
;
1057 if (likely(list_empty(&pl
->list
))) {
1062 nl
= list_first_entry(&pl
->list
, typeof(*nl
), list
);
1064 list_del(&nl
->list
);