Line data Source code
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 : *
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
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"
28 : #include "jhash.h"
29 : #include "network.h"
30 :
31 50 : static uint32_t qobj_hash(const struct qobj_node *node)
32 : {
33 50 : return (uint32_t)node->nid;
34 : }
35 :
36 12 : static int qobj_cmp(const struct qobj_node *na, const struct qobj_node *nb)
37 : {
38 12 : if (na->nid < nb->nid)
39 : return -1;
40 12 : if (na->nid > nb->nid)
41 : return 1;
42 : return 0;
43 : }
44 :
45 221 : DECLARE_HASH(qobj_nodes, struct qobj_node, nodehash,
46 : qobj_cmp, qobj_hash);
47 :
48 : static pthread_rwlock_t nodes_lock;
49 : static struct qobj_nodes_head nodes = { };
50 :
51 :
52 21 : void qobj_reg(struct qobj_node *node, const struct qobj_nodetype *type)
53 : {
54 21 : node->type = type;
55 21 : pthread_rwlock_wrlock(&nodes_lock);
56 21 : do {
57 21 : node->nid = (uint64_t)frr_weak_random();
58 21 : node->nid ^= (uint64_t)frr_weak_random() << 32;
59 42 : } while (!node->nid || qobj_nodes_find(&nodes, node));
60 21 : qobj_nodes_add(&nodes, node);
61 21 : pthread_rwlock_unlock(&nodes_lock);
62 21 : }
63 :
64 17 : void qobj_unreg(struct qobj_node *node)
65 : {
66 17 : pthread_rwlock_wrlock(&nodes_lock);
67 17 : qobj_nodes_del(&nodes, node);
68 17 : pthread_rwlock_unlock(&nodes_lock);
69 17 : }
70 :
71 0 : struct qobj_node *qobj_get(uint64_t id)
72 : {
73 0 : struct qobj_node dummy = {.nid = id}, *rv;
74 0 : pthread_rwlock_rdlock(&nodes_lock);
75 0 : rv = qobj_nodes_find(&nodes, &dummy);
76 0 : pthread_rwlock_unlock(&nodes_lock);
77 0 : return rv;
78 : }
79 :
80 12 : void *qobj_get_typed(uint64_t id, const struct qobj_nodetype *type)
81 : {
82 12 : struct qobj_node dummy = {.nid = id};
83 12 : struct qobj_node *node;
84 12 : void *rv;
85 :
86 12 : pthread_rwlock_rdlock(&nodes_lock);
87 12 : node = qobj_nodes_find(&nodes, &dummy);
88 :
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 12 : if (!node || node->type != type)
96 : rv = NULL;
97 : else
98 12 : rv = (char *)node - node->type->node_member_offset;
99 :
100 12 : pthread_rwlock_unlock(&nodes_lock);
101 12 : return rv;
102 : }
103 :
104 6 : void qobj_init(void)
105 : {
106 6 : pthread_rwlock_init(&nodes_lock, NULL);
107 6 : qobj_nodes_init(&nodes);
108 6 : }
109 :
110 4 : void qobj_finish(void)
111 : {
112 4 : struct qobj_node *node;
113 8 : while ((node = qobj_nodes_pop(&nodes)))
114 4 : qobj_nodes_del(&nodes, node);
115 4 : pthread_rwlock_destroy(&nodes_lock);
116 4 : }
|