]> git.proxmox.com Git - mirror_frr.git/blame - lib/qobj.c
isisd: implemented the 'sequence-number-skipped' notification
[mirror_frr.git] / lib / qobj.c
CommitLineData
1bf9f027
DL
1/*
2 * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc.
3 *
4 * This file is part of Quagga
5 *
6 * Quagga is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * Quagga is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
896014f4
DL
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1bf9f027
DL
19 */
20
21#include <zebra.h>
22
23#include "thread.h"
24#include "memory.h"
25#include "hash.h"
26#include "log.h"
27#include "qobj.h"
bd74dc61 28#include "jhash.h"
1bf9f027 29
297c8f6a 30static pthread_rwlock_t nodes_lock;
1bf9f027
DL
31static struct hash *nodes = NULL;
32
d62a17ae 33static unsigned int qobj_key(void *data)
1bf9f027 34{
d62a17ae 35 struct qobj_node *node = data;
36 return (unsigned int)node->nid;
1bf9f027
DL
37}
38
74df8d6d 39static bool qobj_cmp(const void *a, const void *b)
1bf9f027 40{
d62a17ae 41 const struct qobj_node *na = a, *nb = b;
42 return na->nid == nb->nid;
1bf9f027
DL
43}
44
45void qobj_reg(struct qobj_node *node, struct qobj_nodetype *type)
46{
d62a17ae 47 node->type = type;
48 pthread_rwlock_wrlock(&nodes_lock);
49 do {
50 node->nid = (uint64_t)random();
51 node->nid ^= (uint64_t)random() << 32;
52 } while (!node->nid
53 || hash_get(nodes, node, hash_alloc_intern) != node);
54 pthread_rwlock_unlock(&nodes_lock);
1bf9f027
DL
55}
56
57void qobj_unreg(struct qobj_node *node)
58{
d62a17ae 59 pthread_rwlock_wrlock(&nodes_lock);
60 hash_release(nodes, node);
61 pthread_rwlock_unlock(&nodes_lock);
1bf9f027
DL
62}
63
64struct qobj_node *qobj_get(uint64_t id)
65{
d62a17ae 66 struct qobj_node dummy = {.nid = id}, *rv;
67 pthread_rwlock_rdlock(&nodes_lock);
68 rv = hash_lookup(nodes, &dummy);
69 pthread_rwlock_unlock(&nodes_lock);
70 return rv;
1bf9f027
DL
71}
72
73void *qobj_get_typed(uint64_t id, struct qobj_nodetype *type)
74{
d62a17ae 75 struct qobj_node dummy = {.nid = id};
76 struct qobj_node *node;
77 void *rv;
297c8f6a 78
d62a17ae 79 pthread_rwlock_rdlock(&nodes_lock);
80 node = hash_lookup(nodes, &dummy);
297c8f6a 81
d62a17ae 82 /* note: we explicitly hold the lock until after we have checked the
83 * type.
84 * if the caller holds a lock that for example prevents the deletion of
85 * route-maps, we can still race against a delete of something that
86 * isn't
87 * a route-map. */
88 if (!node || node->type != type)
89 rv = NULL;
90 else
91 rv = (char *)node - node->type->node_member_offset;
297c8f6a 92
d62a17ae 93 pthread_rwlock_unlock(&nodes_lock);
94 return rv;
1bf9f027
DL
95}
96
d62a17ae 97void qobj_init(void)
1bf9f027 98{
d62a17ae 99 if (!nodes) {
100 pthread_rwlock_init(&nodes_lock, NULL);
996c9314 101 nodes = hash_create_size(16, qobj_key, qobj_cmp, "QOBJ Hash");
d62a17ae 102 }
1bf9f027
DL
103}
104
d62a17ae 105void qobj_finish(void)
1bf9f027 106{
d62a17ae 107 hash_clean(nodes, NULL);
108 hash_free(nodes);
109 nodes = NULL;
110 pthread_rwlock_destroy(&nodes_lock);
1bf9f027 111}