Line | Count | Source |
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 | { |
20 | 20 | return (uint32_t)node->nid; |
21 | 20 | } |
22 | | |
23 | | static int qobj_cmp(const struct qobj_node *na, const struct qobj_node *nb) |
24 | 0 | { |
25 | 0 | if (na->nid < nb->nid) |
26 | 0 | return -1; |
27 | 0 | if (na->nid > nb->nid) |
28 | 0 | return 1; |
29 | 0 | return 0; |
30 | 0 | } |
31 | | |
32 | 24 | DECLARE_HASH(qobj_nodes, struct qobj_node, nodehash, qobj.c:qobj_nodes_const_find Line | Count | Source | 32 | | DECLARE_HASH(qobj_nodes, struct qobj_node, nodehash, |
Line | Count | Source | 32 | | DECLARE_HASH(qobj_nodes, struct qobj_node, nodehash, |
Unexecuted instantiation: qobj.c:qobj_nodes_del |
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 | 12 | { |
41 | 12 | node->type = type; |
42 | 12 | pthread_rwlock_wrlock(&nodes_lock); |
43 | 12 | do { |
44 | 12 | node->nid = (uint64_t)frr_weak_random(); |
45 | 12 | node->nid ^= (uint64_t)frr_weak_random() << 32; |
46 | 12 | } while (!node->nid || qobj_nodes_find(&nodes, node)); |
47 | 12 | qobj_nodes_add(&nodes, node); |
48 | 12 | pthread_rwlock_unlock(&nodes_lock); |
49 | 12 | } |
50 | | |
51 | | void qobj_unreg(struct qobj_node *node) |
52 | 0 | { |
53 | 0 | pthread_rwlock_wrlock(&nodes_lock); |
54 | 0 | qobj_nodes_del(&nodes, node); |
55 | 0 | pthread_rwlock_unlock(&nodes_lock); |
56 | 0 | } |
57 | | |
58 | | struct qobj_node *qobj_get(uint64_t id) |
59 | 0 | { |
60 | 0 | struct qobj_node dummy = {.nid = id}, *rv; |
61 | 0 | pthread_rwlock_rdlock(&nodes_lock); |
62 | 0 | rv = qobj_nodes_find(&nodes, &dummy); |
63 | 0 | pthread_rwlock_unlock(&nodes_lock); |
64 | 0 | return rv; |
65 | 0 | } |
66 | | |
67 | | void *qobj_get_typed(uint64_t id, const struct qobj_nodetype *type) |
68 | 0 | { |
69 | 0 | struct qobj_node dummy = {.nid = id}; |
70 | 0 | struct qobj_node *node; |
71 | 0 | void *rv; |
72 | |
|
73 | 0 | pthread_rwlock_rdlock(&nodes_lock); |
74 | 0 | 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 | 0 | if (!node || node->type != type) |
83 | 0 | rv = NULL; |
84 | 0 | else |
85 | 0 | rv = (char *)node - node->type->node_member_offset; |
86 | |
|
87 | 0 | pthread_rwlock_unlock(&nodes_lock); |
88 | 0 | return rv; |
89 | 0 | } |
90 | | |
91 | | void qobj_init(void) |
92 | 5 | { |
93 | 5 | pthread_rwlock_init(&nodes_lock, NULL); |
94 | 5 | qobj_nodes_init(&nodes); |
95 | 5 | } |
96 | | |
97 | | void qobj_finish(void) |
98 | 0 | { |
99 | 0 | struct qobj_node *node; |
100 | 0 | while ((node = qobj_nodes_pop(&nodes))) |
101 | 0 | qobj_nodes_del(&nodes, node); |
102 | 0 | pthread_rwlock_destroy(&nodes_lock); |
103 | 0 | } |