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"
47 #include <net/genetlink.h>
49 #define TIPC_NAMETBL_SIZE 1024 /* must be a power of 2 */
52 * struct name_info - name sequence publication info
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"
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.)
65 struct list_head node_list;
66 struct list_head cluster_list;
67 struct list_head zone_list;
69 u32 cluster_list_size;
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
82 struct name_info *info;
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
91 * @first_free: array index of first unused sub-sequence entry
92 * @ns_list: links to adjacent name sequences in hash chain
93 * @subscriptions: list of subscriptions for this 'type'
94 * @lock: spinlock controlling access to publication lists of all sub-sequences
95 * @rcu: RCU callback head used for deferred freeing
99 struct sub_seq *sseqs;
102 struct hlist_node ns_list;
103 struct list_head subscriptions;
108 static int hash(int x)
110 return x & (TIPC_NAMETBL_SIZE - 1);
114 * publ_create - create a publication structure
116 static struct publication *publ_create(u32 type, u32 lower, u32 upper,
117 u32 scope, u32 node, u32 port_ref,
120 struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
122 pr_warn("Publication creation failure, no memory\n");
131 publ->ref = port_ref;
133 INIT_LIST_HEAD(&publ->pport_list);
138 * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
140 static struct sub_seq *tipc_subseq_alloc(u32 cnt)
142 return kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC);
146 * tipc_nameseq_create - create a name sequence structure for the specified 'type'
148 * Allocates a single sub-sequence structure and sets it to all 0's.
150 static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head)
152 struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC);
153 struct sub_seq *sseq = tipc_subseq_alloc(1);
155 if (!nseq || !sseq) {
156 pr_warn("Name sequence creation failed, no memory\n");
162 spin_lock_init(&nseq->lock);
166 INIT_HLIST_NODE(&nseq->ns_list);
167 INIT_LIST_HEAD(&nseq->subscriptions);
168 hlist_add_head_rcu(&nseq->ns_list, seq_head);
173 * nameseq_find_subseq - find sub-sequence (if any) matching a name instance
175 * Very time-critical, so binary searches through sub-sequence array.
177 static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq,
180 struct sub_seq *sseqs = nseq->sseqs;
182 int high = nseq->first_free - 1;
185 while (low <= high) {
186 mid = (low + high) / 2;
187 if (instance < sseqs[mid].lower)
189 else if (instance > sseqs[mid].upper)
198 * nameseq_locate_subseq - determine position of name instance in sub-sequence
200 * Returns index in sub-sequence array of the entry that contains the specified
201 * instance value; if no entry contains that value, returns the position
202 * where a new entry for it would be inserted in the array.
204 * Note: Similar to binary search code for locating a sub-sequence.
206 static u32 nameseq_locate_subseq(struct name_seq *nseq, u32 instance)
208 struct sub_seq *sseqs = nseq->sseqs;
210 int high = nseq->first_free - 1;
213 while (low <= high) {
214 mid = (low + high) / 2;
215 if (instance < sseqs[mid].lower)
217 else if (instance > sseqs[mid].upper)
226 * tipc_nameseq_insert_publ
228 static struct publication *tipc_nameseq_insert_publ(struct net *net,
229 struct name_seq *nseq,
231 u32 upper, u32 scope,
232 u32 node, u32 port, u32 key)
234 struct tipc_subscription *s;
235 struct tipc_subscription *st;
236 struct publication *publ;
237 struct sub_seq *sseq;
238 struct name_info *info;
239 int created_subseq = 0;
241 sseq = nameseq_find_subseq(nseq, lower);
244 /* Lower end overlaps existing entry => need an exact match */
245 if ((sseq->lower != lower) || (sseq->upper != upper)) {
251 /* Check if an identical publication already exists */
252 list_for_each_entry(publ, &info->zone_list, zone_list) {
253 if ((publ->ref == port) && (publ->key == key) &&
254 (!publ->node || (publ->node == node)))
259 struct sub_seq *freesseq;
261 /* Find where lower end should be inserted */
262 inspos = nameseq_locate_subseq(nseq, lower);
264 /* Fail if upper end overlaps into an existing entry */
265 if ((inspos < nseq->first_free) &&
266 (upper >= nseq->sseqs[inspos].lower)) {
270 /* Ensure there is space for new sub-sequence */
271 if (nseq->first_free == nseq->alloc) {
272 struct sub_seq *sseqs = tipc_subseq_alloc(nseq->alloc * 2);
275 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
279 memcpy(sseqs, nseq->sseqs,
280 nseq->alloc * sizeof(struct sub_seq));
286 info = kzalloc(sizeof(*info), GFP_ATOMIC);
288 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
293 INIT_LIST_HEAD(&info->node_list);
294 INIT_LIST_HEAD(&info->cluster_list);
295 INIT_LIST_HEAD(&info->zone_list);
297 /* Insert new sub-sequence */
298 sseq = &nseq->sseqs[inspos];
299 freesseq = &nseq->sseqs[nseq->first_free];
300 memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof(*sseq));
301 memset(sseq, 0, sizeof(*sseq));
309 /* Insert a publication */
310 publ = publ_create(type, lower, upper, scope, node, port, key);
314 list_add(&publ->zone_list, &info->zone_list);
315 info->zone_list_size++;
317 if (in_own_cluster(net, node)) {
318 list_add(&publ->cluster_list, &info->cluster_list);
319 info->cluster_list_size++;
322 if (in_own_node(net, node)) {
323 list_add(&publ->node_list, &info->node_list);
324 info->node_list_size++;
327 /* Any subscriptions waiting for notification? */
328 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
329 tipc_subscrp_report_overlap(s, publ->lower, publ->upper,
330 TIPC_PUBLISHED, publ->ref,
331 publ->node, created_subseq);
337 * tipc_nameseq_remove_publ
339 * NOTE: There may be cases where TIPC is asked to remove a publication
340 * that is not in the name table. For example, if another node issues a
341 * publication for a name sequence that overlaps an existing name sequence
342 * the publication will not be recorded, which means the publication won't
343 * be found when the name sequence is later withdrawn by that node.
344 * A failed withdraw request simply returns a failure indication and lets the
345 * caller issue any error or warning messages associated with such a problem.
347 static struct publication *tipc_nameseq_remove_publ(struct net *net,
348 struct name_seq *nseq,
352 struct publication *publ;
353 struct sub_seq *sseq = nameseq_find_subseq(nseq, inst);
354 struct name_info *info;
355 struct sub_seq *free;
356 struct tipc_subscription *s, *st;
357 int removed_subseq = 0;
364 /* Locate publication, if it exists */
365 list_for_each_entry(publ, &info->zone_list, zone_list) {
366 if ((publ->key == key) && (publ->ref == ref) &&
367 (!publ->node || (publ->node == node)))
373 /* Remove publication from zone scope list */
374 list_del(&publ->zone_list);
375 info->zone_list_size--;
377 /* Remove publication from cluster scope list, if present */
378 if (in_own_cluster(net, node)) {
379 list_del(&publ->cluster_list);
380 info->cluster_list_size--;
383 /* Remove publication from node scope list, if present */
384 if (in_own_node(net, node)) {
385 list_del(&publ->node_list);
386 info->node_list_size--;
389 /* Contract subseq list if no more publications for that subseq */
390 if (list_empty(&info->zone_list)) {
392 free = &nseq->sseqs[nseq->first_free--];
393 memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq));
397 /* Notify any waiting subscriptions */
398 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
399 tipc_subscrp_report_overlap(s, publ->lower, publ->upper,
400 TIPC_WITHDRAWN, publ->ref,
401 publ->node, removed_subseq);
408 * tipc_nameseq_subscribe - attach a subscription, and issue
409 * the prescribed number of events if there is any sub-
410 * sequence overlapping with the requested sequence
412 static void tipc_nameseq_subscribe(struct name_seq *nseq,
413 struct tipc_subscription *s)
415 struct sub_seq *sseq = nseq->sseqs;
416 struct tipc_name_seq ns;
418 tipc_subscrp_convert_seq(&s->evt.s.seq, s->swap, &ns);
421 list_add(&s->nameseq_list, &nseq->subscriptions);
426 while (sseq != &nseq->sseqs[nseq->first_free]) {
427 if (tipc_subscrp_check_overlap(&ns, sseq->lower, sseq->upper)) {
428 struct publication *crs;
429 struct name_info *info = sseq->info;
432 list_for_each_entry(crs, &info->zone_list, zone_list) {
433 tipc_subscrp_report_overlap(s, sseq->lower,
445 static struct name_seq *nametbl_find_seq(struct net *net, u32 type)
447 struct tipc_net *tn = net_generic(net, tipc_net_id);
448 struct hlist_head *seq_head;
451 seq_head = &tn->nametbl->seq_hlist[hash(type)];
452 hlist_for_each_entry_rcu(ns, seq_head, ns_list) {
453 if (ns->type == type)
460 struct publication *tipc_nametbl_insert_publ(struct net *net, u32 type,
461 u32 lower, u32 upper, u32 scope,
462 u32 node, u32 port, u32 key)
464 struct tipc_net *tn = net_generic(net, tipc_net_id);
465 struct publication *publ;
466 struct name_seq *seq = nametbl_find_seq(net, type);
467 int index = hash(type);
469 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE) ||
471 pr_debug("Failed to publish illegal {%u,%u,%u} with scope %u\n",
472 type, lower, upper, scope);
477 seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
481 spin_lock_bh(&seq->lock);
482 publ = tipc_nameseq_insert_publ(net, seq, type, lower, upper,
483 scope, node, port, key);
484 spin_unlock_bh(&seq->lock);
488 struct publication *tipc_nametbl_remove_publ(struct net *net, u32 type,
489 u32 lower, u32 node, u32 ref,
492 struct publication *publ;
493 struct name_seq *seq = nametbl_find_seq(net, type);
498 spin_lock_bh(&seq->lock);
499 publ = tipc_nameseq_remove_publ(net, seq, lower, node, ref, key);
500 if (!seq->first_free && list_empty(&seq->subscriptions)) {
501 hlist_del_init_rcu(&seq->ns_list);
503 spin_unlock_bh(&seq->lock);
507 spin_unlock_bh(&seq->lock);
512 * tipc_nametbl_translate - perform name translation
514 * On entry, 'destnode' is the search domain used during translation.
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
524 u32 tipc_nametbl_translate(struct net *net, u32 type, u32 instance,
527 struct tipc_net *tn = net_generic(net, tipc_net_id);
528 struct sub_seq *sseq;
529 struct name_info *info;
530 struct publication *publ;
531 struct name_seq *seq;
535 if (!tipc_in_scope(*destnode, tn->own_addr))
539 seq = nametbl_find_seq(net, type);
542 spin_lock_bh(&seq->lock);
543 sseq = nameseq_find_subseq(seq, instance);
548 /* Closest-First Algorithm */
549 if (likely(!*destnode)) {
550 if (!list_empty(&info->node_list)) {
551 publ = list_first_entry(&info->node_list,
554 list_move_tail(&publ->node_list,
556 } else if (!list_empty(&info->cluster_list)) {
557 publ = list_first_entry(&info->cluster_list,
560 list_move_tail(&publ->cluster_list,
561 &info->cluster_list);
563 publ = list_first_entry(&info->zone_list,
566 list_move_tail(&publ->zone_list,
571 /* Round-Robin Algorithm */
572 else if (*destnode == tn->own_addr) {
573 if (list_empty(&info->node_list))
575 publ = list_first_entry(&info->node_list, struct publication,
577 list_move_tail(&publ->node_list, &info->node_list);
578 } else if (in_own_cluster_exact(net, *destnode)) {
579 if (list_empty(&info->cluster_list))
581 publ = list_first_entry(&info->cluster_list, struct publication,
583 list_move_tail(&publ->cluster_list, &info->cluster_list);
585 publ = list_first_entry(&info->zone_list, struct publication,
587 list_move_tail(&publ->zone_list, &info->zone_list);
593 spin_unlock_bh(&seq->lock);
600 bool tipc_nametbl_lookup(struct net *net, u32 type, u32 instance, u32 domain,
601 struct list_head *dsts, int *dstcnt, u32 exclude,
604 u32 self = tipc_own_addr(net);
605 struct publication *publ;
606 struct name_info *info;
607 struct name_seq *seq;
608 struct sub_seq *sseq;
610 if (!tipc_in_scope(domain, self))
615 seq = nametbl_find_seq(net, type);
618 spin_lock_bh(&seq->lock);
619 sseq = nameseq_find_subseq(seq, instance);
622 list_for_each_entry(publ, &info->zone_list, zone_list) {
623 if (!tipc_in_scope(domain, publ->node))
625 if (publ->ref == exclude && publ->node == self)
627 tipc_dest_push(dsts, publ->node, publ->ref);
631 list_move_tail(&publ->zone_list, &info->zone_list);
635 spin_unlock_bh(&seq->lock);
638 return !list_empty(dsts);
641 int tipc_nametbl_mc_translate(struct net *net, u32 type, u32 lower, u32 upper,
642 u32 limit, struct list_head *dports)
644 struct name_seq *seq;
645 struct sub_seq *sseq;
646 struct sub_seq *sseq_stop;
647 struct name_info *info;
651 seq = nametbl_find_seq(net, type);
655 spin_lock_bh(&seq->lock);
656 sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
657 sseq_stop = seq->sseqs + seq->first_free;
658 for (; sseq != sseq_stop; sseq++) {
659 struct publication *publ;
661 if (sseq->lower > upper)
665 list_for_each_entry(publ, &info->node_list, node_list) {
666 if (publ->scope <= limit)
667 tipc_dest_push(dports, 0, publ->ref);
670 if (info->cluster_list_size != info->node_list_size)
673 spin_unlock_bh(&seq->lock);
679 /* tipc_nametbl_lookup_dst_nodes - find broadcast destination nodes
680 * - Creates list of nodes that overlap the given multicast address
681 * - Determines if any node local ports overlap
683 void tipc_nametbl_lookup_dst_nodes(struct net *net, u32 type, u32 lower,
684 u32 upper, u32 domain,
685 struct tipc_nlist *nodes)
687 struct sub_seq *sseq, *stop;
688 struct publication *publ;
689 struct name_info *info;
690 struct name_seq *seq;
693 seq = nametbl_find_seq(net, type);
697 spin_lock_bh(&seq->lock);
698 sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
699 stop = seq->sseqs + seq->first_free;
700 for (; sseq != stop && sseq->lower <= upper; sseq++) {
702 list_for_each_entry(publ, &info->zone_list, zone_list) {
703 if (tipc_in_scope(domain, publ->node))
704 tipc_nlist_add(nodes, publ->node);
707 spin_unlock_bh(&seq->lock);
712 /* tipc_nametbl_build_group - build list of communication group members
714 void tipc_nametbl_build_group(struct net *net, struct tipc_group *grp,
715 u32 type, u32 domain)
717 struct sub_seq *sseq, *stop;
718 struct name_info *info;
719 struct publication *p;
720 struct name_seq *seq;
723 seq = nametbl_find_seq(net, type);
727 spin_lock_bh(&seq->lock);
729 stop = seq->sseqs + seq->first_free;
730 for (; sseq != stop; sseq++) {
732 list_for_each_entry(p, &info->zone_list, zone_list) {
733 if (!tipc_in_scope(domain, p->node))
735 tipc_group_add_member(grp, p->node, p->ref);
738 spin_unlock_bh(&seq->lock);
744 * tipc_nametbl_publish - add name publication to network name tables
746 struct publication *tipc_nametbl_publish(struct net *net, u32 type, u32 lower,
747 u32 upper, u32 scope, u32 port_ref,
750 struct publication *publ;
751 struct sk_buff *buf = NULL;
752 struct tipc_net *tn = net_generic(net, tipc_net_id);
754 spin_lock_bh(&tn->nametbl_lock);
755 if (tn->nametbl->local_publ_count >= TIPC_MAX_PUBLICATIONS) {
756 pr_warn("Publication failed, local publication limit reached (%u)\n",
757 TIPC_MAX_PUBLICATIONS);
758 spin_unlock_bh(&tn->nametbl_lock);
762 publ = tipc_nametbl_insert_publ(net, type, lower, upper, scope,
763 tn->own_addr, port_ref, key);
765 tn->nametbl->local_publ_count++;
766 buf = tipc_named_publish(net, publ);
767 /* Any pending external events? */
768 tipc_named_process_backlog(net);
770 spin_unlock_bh(&tn->nametbl_lock);
773 tipc_node_broadcast(net, buf);
778 * tipc_nametbl_withdraw - withdraw name publication from network name tables
780 int tipc_nametbl_withdraw(struct net *net, u32 type, u32 lower, u32 ref,
783 struct publication *publ;
784 struct sk_buff *skb = NULL;
785 struct tipc_net *tn = net_generic(net, tipc_net_id);
787 spin_lock_bh(&tn->nametbl_lock);
788 publ = tipc_nametbl_remove_publ(net, type, lower, tn->own_addr,
791 tn->nametbl->local_publ_count--;
792 skb = tipc_named_withdraw(net, publ);
793 /* Any pending external events? */
794 tipc_named_process_backlog(net);
795 list_del_init(&publ->pport_list);
796 kfree_rcu(publ, rcu);
798 pr_err("Unable to remove local publication\n"
799 "(type=%u, lower=%u, ref=%u, key=%u)\n",
800 type, lower, ref, key);
802 spin_unlock_bh(&tn->nametbl_lock);
805 tipc_node_broadcast(net, skb);
812 * tipc_nametbl_subscribe - add a subscription object to the name table
814 void tipc_nametbl_subscribe(struct tipc_subscription *s)
816 struct tipc_net *tn = net_generic(s->net, tipc_net_id);
817 u32 type = tipc_subscrp_convert_seq_type(s->evt.s.seq.type, s->swap);
818 int index = hash(type);
819 struct name_seq *seq;
820 struct tipc_name_seq ns;
822 spin_lock_bh(&tn->nametbl_lock);
823 seq = nametbl_find_seq(s->net, type);
825 seq = tipc_nameseq_create(type, &tn->nametbl->seq_hlist[index]);
827 spin_lock_bh(&seq->lock);
828 tipc_nameseq_subscribe(seq, s);
829 spin_unlock_bh(&seq->lock);
831 tipc_subscrp_convert_seq(&s->evt.s.seq, s->swap, &ns);
832 pr_warn("Failed to create subscription for {%u,%u,%u}\n",
833 ns.type, ns.lower, ns.upper);
835 spin_unlock_bh(&tn->nametbl_lock);
839 * tipc_nametbl_unsubscribe - remove a subscription object from name table
841 void tipc_nametbl_unsubscribe(struct tipc_subscription *s)
843 struct tipc_net *tn = net_generic(s->net, tipc_net_id);
844 struct name_seq *seq;
845 u32 type = tipc_subscrp_convert_seq_type(s->evt.s.seq.type, s->swap);
847 spin_lock_bh(&tn->nametbl_lock);
848 seq = nametbl_find_seq(s->net, type);
850 spin_lock_bh(&seq->lock);
851 list_del_init(&s->nameseq_list);
853 if (!seq->first_free && list_empty(&seq->subscriptions)) {
854 hlist_del_init_rcu(&seq->ns_list);
856 spin_unlock_bh(&seq->lock);
859 spin_unlock_bh(&seq->lock);
862 spin_unlock_bh(&tn->nametbl_lock);
865 int tipc_nametbl_init(struct net *net)
867 struct tipc_net *tn = net_generic(net, tipc_net_id);
868 struct name_table *tipc_nametbl;
871 tipc_nametbl = kzalloc(sizeof(*tipc_nametbl), GFP_ATOMIC);
875 for (i = 0; i < TIPC_NAMETBL_SIZE; i++)
876 INIT_HLIST_HEAD(&tipc_nametbl->seq_hlist[i]);
878 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_ZONE_SCOPE]);
879 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_CLUSTER_SCOPE]);
880 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_NODE_SCOPE]);
881 tn->nametbl = tipc_nametbl;
882 spin_lock_init(&tn->nametbl_lock);
887 * tipc_purge_publications - remove all publications for a given type
889 * tipc_nametbl_lock must be held when calling this function
891 static void tipc_purge_publications(struct net *net, struct name_seq *seq)
893 struct publication *publ, *safe;
894 struct sub_seq *sseq;
895 struct name_info *info;
897 spin_lock_bh(&seq->lock);
900 list_for_each_entry_safe(publ, safe, &info->zone_list, zone_list) {
901 tipc_nameseq_remove_publ(net, seq, publ->lower, publ->node,
902 publ->ref, publ->key);
903 kfree_rcu(publ, rcu);
905 hlist_del_init_rcu(&seq->ns_list);
907 spin_unlock_bh(&seq->lock);
912 void tipc_nametbl_stop(struct net *net)
915 struct name_seq *seq;
916 struct hlist_head *seq_head;
917 struct tipc_net *tn = net_generic(net, tipc_net_id);
918 struct name_table *tipc_nametbl = tn->nametbl;
920 /* Verify name table is empty and purge any lingering
921 * publications, then release the name table
923 spin_lock_bh(&tn->nametbl_lock);
924 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
925 if (hlist_empty(&tipc_nametbl->seq_hlist[i]))
927 seq_head = &tipc_nametbl->seq_hlist[i];
928 hlist_for_each_entry_rcu(seq, seq_head, ns_list) {
929 tipc_purge_publications(net, seq);
932 spin_unlock_bh(&tn->nametbl_lock);
939 static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
940 struct name_seq *seq,
941 struct sub_seq *sseq, u32 *last_publ)
944 struct nlattr *attrs;
946 struct publication *p;
949 list_for_each_entry(p, &sseq->info->zone_list, zone_list)
950 if (p->key == *last_publ)
952 if (p->key != *last_publ)
955 p = list_first_entry(&sseq->info->zone_list, struct publication,
959 list_for_each_entry_from(p, &sseq->info->zone_list, zone_list) {
962 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq,
963 &tipc_genl_family, NLM_F_MULTI,
964 TIPC_NL_NAME_TABLE_GET);
968 attrs = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE);
972 publ = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE_PUBL);
976 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, seq->type))
978 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sseq->lower))
980 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sseq->upper))
982 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope))
984 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->node))
986 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->ref))
988 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key))
991 nla_nest_end(msg->skb, publ);
992 nla_nest_end(msg->skb, attrs);
993 genlmsg_end(msg->skb, hdr);
1000 nla_nest_cancel(msg->skb, publ);
1002 nla_nest_cancel(msg->skb, attrs);
1004 genlmsg_cancel(msg->skb, hdr);
1009 static int __tipc_nl_subseq_list(struct tipc_nl_msg *msg, struct name_seq *seq,
1010 u32 *last_lower, u32 *last_publ)
1012 struct sub_seq *sseq;
1013 struct sub_seq *sseq_start;
1017 sseq_start = nameseq_find_subseq(seq, *last_lower);
1021 sseq_start = seq->sseqs;
1024 for (sseq = sseq_start; sseq != &seq->sseqs[seq->first_free]; sseq++) {
1025 err = __tipc_nl_add_nametable_publ(msg, seq, sseq, last_publ);
1027 *last_lower = sseq->lower;
1036 static int tipc_nl_seq_list(struct net *net, struct tipc_nl_msg *msg,
1037 u32 *last_type, u32 *last_lower, u32 *last_publ)
1039 struct tipc_net *tn = net_generic(net, tipc_net_id);
1040 struct hlist_head *seq_head;
1041 struct name_seq *seq = NULL;
1046 i = hash(*last_type);
1050 for (; i < TIPC_NAMETBL_SIZE; i++) {
1051 seq_head = &tn->nametbl->seq_hlist[i];
1054 seq = nametbl_find_seq(net, *last_type);
1058 hlist_for_each_entry_rcu(seq, seq_head, ns_list)
1064 hlist_for_each_entry_from_rcu(seq, ns_list) {
1065 spin_lock_bh(&seq->lock);
1066 err = __tipc_nl_subseq_list(msg, seq, last_lower,
1070 *last_type = seq->type;
1071 spin_unlock_bh(&seq->lock);
1074 spin_unlock_bh(&seq->lock);
1081 int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb)
1084 int done = cb->args[3];
1085 u32 last_type = cb->args[0];
1086 u32 last_lower = cb->args[1];
1087 u32 last_publ = cb->args[2];
1088 struct net *net = sock_net(skb->sk);
1089 struct tipc_nl_msg msg;
1095 msg.portid = NETLINK_CB(cb->skb).portid;
1096 msg.seq = cb->nlh->nlmsg_seq;
1099 err = tipc_nl_seq_list(net, &msg, &last_type, &last_lower, &last_publ);
1102 } else if (err != -EMSGSIZE) {
1103 /* We never set seq or call nl_dump_check_consistent() this
1104 * means that setting prev_seq here will cause the consistence
1105 * check to fail in the netlink callback handler. Resulting in
1106 * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
1113 cb->args[0] = last_type;
1114 cb->args[1] = last_lower;
1115 cb->args[2] = last_publ;
1121 struct tipc_dest *tipc_dest_find(struct list_head *l, u32 node, u32 port)
1123 u64 value = (u64)node << 32 | port;
1124 struct tipc_dest *dst;
1126 list_for_each_entry(dst, l, list) {
1127 if (dst->value != value)
1134 bool tipc_dest_push(struct list_head *l, u32 node, u32 port)
1136 u64 value = (u64)node << 32 | port;
1137 struct tipc_dest *dst;
1139 if (tipc_dest_find(l, node, port))
1142 dst = kmalloc(sizeof(*dst), GFP_ATOMIC);
1146 list_add(&dst->list, l);
1150 bool tipc_dest_pop(struct list_head *l, u32 *node, u32 *port)
1152 struct tipc_dest *dst;
1156 dst = list_first_entry(l, typeof(*dst), list);
1161 list_del(&dst->list);
1166 bool tipc_dest_del(struct list_head *l, u32 node, u32 port)
1168 struct tipc_dest *dst;
1170 dst = tipc_dest_find(l, node, port);
1173 list_del(&dst->list);
1178 void tipc_dest_list_purge(struct list_head *l)
1180 struct tipc_dest *dst, *tmp;
1182 list_for_each_entry_safe(dst, tmp, l, list) {
1183 list_del(&dst->list);
1188 int tipc_dest_list_len(struct list_head *l)
1190 struct tipc_dest *dst;
1193 list_for_each_entry(dst, l, list) {