]>
Commit | Line | Data |
---|---|---|
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" |
5920b3eb | 29 | #include "network.h" |
1bf9f027 | 30 | |
679b1649 | 31 | static uint32_t qobj_hash(const struct qobj_node *node) |
1bf9f027 | 32 | { |
679b1649 | 33 | return (uint32_t)node->nid; |
1bf9f027 DL |
34 | } |
35 | ||
679b1649 | 36 | static int qobj_cmp(const struct qobj_node *na, const struct qobj_node *nb) |
1bf9f027 | 37 | { |
679b1649 DL |
38 | if (na->nid < nb->nid) |
39 | return -1; | |
40 | if (na->nid > nb->nid) | |
41 | return 1; | |
42 | return 0; | |
1bf9f027 DL |
43 | } |
44 | ||
679b1649 | 45 | DECLARE_HASH(qobj_nodes, struct qobj_node, nodehash, |
960b9a53 | 46 | qobj_cmp, qobj_hash); |
679b1649 DL |
47 | |
48 | static pthread_rwlock_t nodes_lock; | |
49 | static struct qobj_nodes_head nodes = { }; | |
50 | ||
51 | ||
154e9ca1 | 52 | void qobj_reg(struct qobj_node *node, const struct qobj_nodetype *type) |
1bf9f027 | 53 | { |
d62a17ae | 54 | node->type = type; |
55 | pthread_rwlock_wrlock(&nodes_lock); | |
56 | do { | |
5920b3eb RZ |
57 | node->nid = (uint64_t)frr_weak_random(); |
58 | node->nid ^= (uint64_t)frr_weak_random() << 32; | |
679b1649 DL |
59 | } while (!node->nid || qobj_nodes_find(&nodes, node)); |
60 | qobj_nodes_add(&nodes, node); | |
d62a17ae | 61 | pthread_rwlock_unlock(&nodes_lock); |
1bf9f027 DL |
62 | } |
63 | ||
64 | void qobj_unreg(struct qobj_node *node) | |
65 | { | |
d62a17ae | 66 | pthread_rwlock_wrlock(&nodes_lock); |
679b1649 | 67 | qobj_nodes_del(&nodes, node); |
d62a17ae | 68 | pthread_rwlock_unlock(&nodes_lock); |
1bf9f027 DL |
69 | } |
70 | ||
71 | struct qobj_node *qobj_get(uint64_t id) | |
72 | { | |
d62a17ae | 73 | struct qobj_node dummy = {.nid = id}, *rv; |
74 | pthread_rwlock_rdlock(&nodes_lock); | |
679b1649 | 75 | rv = qobj_nodes_find(&nodes, &dummy); |
d62a17ae | 76 | pthread_rwlock_unlock(&nodes_lock); |
77 | return rv; | |
1bf9f027 DL |
78 | } |
79 | ||
154e9ca1 | 80 | void *qobj_get_typed(uint64_t id, const struct qobj_nodetype *type) |
1bf9f027 | 81 | { |
d62a17ae | 82 | struct qobj_node dummy = {.nid = id}; |
83 | struct qobj_node *node; | |
84 | void *rv; | |
297c8f6a | 85 | |
d62a17ae | 86 | pthread_rwlock_rdlock(&nodes_lock); |
679b1649 | 87 | node = qobj_nodes_find(&nodes, &dummy); |
297c8f6a | 88 | |
d62a17ae | 89 | /* note: we explicitly hold the lock until after we have checked the |
90 | * type. | |
91 | * if the caller holds a lock that for example prevents the deletion of | |
92 | * route-maps, we can still race against a delete of something that | |
93 | * isn't | |
94 | * a route-map. */ | |
95 | if (!node || node->type != type) | |
96 | rv = NULL; | |
97 | else | |
98 | rv = (char *)node - node->type->node_member_offset; | |
297c8f6a | 99 | |
d62a17ae | 100 | pthread_rwlock_unlock(&nodes_lock); |
101 | return rv; | |
1bf9f027 DL |
102 | } |
103 | ||
d62a17ae | 104 | void qobj_init(void) |
1bf9f027 | 105 | { |
679b1649 DL |
106 | pthread_rwlock_init(&nodes_lock, NULL); |
107 | qobj_nodes_init(&nodes); | |
1bf9f027 DL |
108 | } |
109 | ||
d62a17ae | 110 | void qobj_finish(void) |
1bf9f027 | 111 | { |
679b1649 DL |
112 | struct qobj_node *node; |
113 | while ((node = qobj_nodes_pop(&nodes))) | |
114 | qobj_nodes_del(&nodes, node); | |
d62a17ae | 115 | pthread_rwlock_destroy(&nodes_lock); |
1bf9f027 | 116 | } |