]>
Commit | Line | Data |
---|---|---|
1 | // SPDX-License-Identifier: GPL-2.0-or-later | |
2 | /* | |
3 | * Copyright (c) 2015-16 David Lamparter, for NetDEF, Inc. | |
4 | * | |
5 | * This file is part of Quagga | |
6 | */ | |
7 | ||
8 | #include <zebra.h> | |
9 | ||
10 | #include "frrevent.h" | |
11 | #include "memory.h" | |
12 | #include "hash.h" | |
13 | #include "log.h" | |
14 | #include "qobj.h" | |
15 | #include "jhash.h" | |
16 | #include "network.h" | |
17 | ||
18 | static uint32_t qobj_hash(const struct qobj_node *node) | |
19 | { | |
20 | return (uint32_t)node->nid; | |
21 | } | |
22 | ||
23 | static int qobj_cmp(const struct qobj_node *na, const struct qobj_node *nb) | |
24 | { | |
25 | if (na->nid < nb->nid) | |
26 | return -1; | |
27 | if (na->nid > nb->nid) | |
28 | return 1; | |
29 | return 0; | |
30 | } | |
31 | ||
32 | DECLARE_HASH(qobj_nodes, struct qobj_node, nodehash, | |
33 | qobj_cmp, qobj_hash); | |
34 | ||
35 | static pthread_rwlock_t nodes_lock; | |
36 | static struct qobj_nodes_head nodes = { }; | |
37 | ||
38 | ||
39 | void qobj_reg(struct qobj_node *node, const struct qobj_nodetype *type) | |
40 | { | |
41 | node->type = type; | |
42 | pthread_rwlock_wrlock(&nodes_lock); | |
43 | do { | |
44 | node->nid = (uint64_t)frr_weak_random(); | |
45 | node->nid ^= (uint64_t)frr_weak_random() << 32; | |
46 | } while (!node->nid || qobj_nodes_find(&nodes, node)); | |
47 | qobj_nodes_add(&nodes, node); | |
48 | pthread_rwlock_unlock(&nodes_lock); | |
49 | } | |
50 | ||
51 | void qobj_unreg(struct qobj_node *node) | |
52 | { | |
53 | pthread_rwlock_wrlock(&nodes_lock); | |
54 | qobj_nodes_del(&nodes, node); | |
55 | pthread_rwlock_unlock(&nodes_lock); | |
56 | } | |
57 | ||
58 | struct qobj_node *qobj_get(uint64_t id) | |
59 | { | |
60 | struct qobj_node dummy = {.nid = id}, *rv; | |
61 | pthread_rwlock_rdlock(&nodes_lock); | |
62 | rv = qobj_nodes_find(&nodes, &dummy); | |
63 | pthread_rwlock_unlock(&nodes_lock); | |
64 | return rv; | |
65 | } | |
66 | ||
67 | void *qobj_get_typed(uint64_t id, const struct qobj_nodetype *type) | |
68 | { | |
69 | struct qobj_node dummy = {.nid = id}; | |
70 | struct qobj_node *node; | |
71 | void *rv; | |
72 | ||
73 | pthread_rwlock_rdlock(&nodes_lock); | |
74 | node = qobj_nodes_find(&nodes, &dummy); | |
75 | ||
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; | |
86 | ||
87 | pthread_rwlock_unlock(&nodes_lock); | |
88 | return rv; | |
89 | } | |
90 | ||
91 | void qobj_init(void) | |
92 | { | |
93 | pthread_rwlock_init(&nodes_lock, NULL); | |
94 | qobj_nodes_init(&nodes); | |
95 | } | |
96 | ||
97 | void qobj_finish(void) | |
98 | { | |
99 | struct qobj_node *node; | |
100 | while ((node = qobj_nodes_pop(&nodes))) | |
101 | qobj_nodes_del(&nodes, node); | |
102 | pthread_rwlock_destroy(&nodes_lock); | |
103 | } |