]> git.proxmox.com Git - mirror_frr.git/blame - lib/qobj.c
*: auto-convert to SPDX License IDs
[mirror_frr.git] / lib / qobj.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
1bf9f027
DL
2/*
3 * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc.
4 *
5 * This file is part of Quagga
1bf9f027
DL
6 */
7
8#include <zebra.h>
9
10#include "thread.h"
11#include "memory.h"
12#include "hash.h"
13#include "log.h"
14#include "qobj.h"
bd74dc61 15#include "jhash.h"
5920b3eb 16#include "network.h"
1bf9f027 17
679b1649 18static uint32_t qobj_hash(const struct qobj_node *node)
1bf9f027 19{
679b1649 20 return (uint32_t)node->nid;
1bf9f027
DL
21}
22
679b1649 23static int qobj_cmp(const struct qobj_node *na, const struct qobj_node *nb)
1bf9f027 24{
679b1649
DL
25 if (na->nid < nb->nid)
26 return -1;
27 if (na->nid > nb->nid)
28 return 1;
29 return 0;
1bf9f027
DL
30}
31
679b1649 32DECLARE_HASH(qobj_nodes, struct qobj_node, nodehash,
960b9a53 33 qobj_cmp, qobj_hash);
679b1649
DL
34
35static pthread_rwlock_t nodes_lock;
36static struct qobj_nodes_head nodes = { };
37
38
154e9ca1 39void qobj_reg(struct qobj_node *node, const struct qobj_nodetype *type)
1bf9f027 40{
d62a17ae 41 node->type = type;
42 pthread_rwlock_wrlock(&nodes_lock);
43 do {
5920b3eb
RZ
44 node->nid = (uint64_t)frr_weak_random();
45 node->nid ^= (uint64_t)frr_weak_random() << 32;
679b1649
DL
46 } while (!node->nid || qobj_nodes_find(&nodes, node));
47 qobj_nodes_add(&nodes, node);
d62a17ae 48 pthread_rwlock_unlock(&nodes_lock);
1bf9f027
DL
49}
50
51void qobj_unreg(struct qobj_node *node)
52{
d62a17ae 53 pthread_rwlock_wrlock(&nodes_lock);
679b1649 54 qobj_nodes_del(&nodes, node);
d62a17ae 55 pthread_rwlock_unlock(&nodes_lock);
1bf9f027
DL
56}
57
58struct qobj_node *qobj_get(uint64_t id)
59{
d62a17ae 60 struct qobj_node dummy = {.nid = id}, *rv;
61 pthread_rwlock_rdlock(&nodes_lock);
679b1649 62 rv = qobj_nodes_find(&nodes, &dummy);
d62a17ae 63 pthread_rwlock_unlock(&nodes_lock);
64 return rv;
1bf9f027
DL
65}
66
154e9ca1 67void *qobj_get_typed(uint64_t id, const struct qobj_nodetype *type)
1bf9f027 68{
d62a17ae 69 struct qobj_node dummy = {.nid = id};
70 struct qobj_node *node;
71 void *rv;
297c8f6a 72
d62a17ae 73 pthread_rwlock_rdlock(&nodes_lock);
679b1649 74 node = qobj_nodes_find(&nodes, &dummy);
297c8f6a 75
d62a17ae 76 /* note: we explicitly hold the lock until after we have checked the
77 * type.
78 * if the caller holds a lock that for example prevents the deletion of
79 * route-maps, we can still race against a delete of something that
80 * isn't
81 * a route-map. */
82 if (!node || node->type != type)
83 rv = NULL;
84 else
85 rv = (char *)node - node->type->node_member_offset;
297c8f6a 86
d62a17ae 87 pthread_rwlock_unlock(&nodes_lock);
88 return rv;
1bf9f027
DL
89}
90
d62a17ae 91void qobj_init(void)
1bf9f027 92{
679b1649
DL
93 pthread_rwlock_init(&nodes_lock, NULL);
94 qobj_nodes_init(&nodes);
1bf9f027
DL
95}
96
d62a17ae 97void qobj_finish(void)
1bf9f027 98{
679b1649
DL
99 struct qobj_node *node;
100 while ((node = qobj_nodes_pop(&nodes)))
101 qobj_nodes_del(&nodes, node);
d62a17ae 102 pthread_rwlock_destroy(&nodes_lock);
1bf9f027 103}