blob: a8874fb90e46bde6cf4239ddd9d92076fad1f787 [file] [log] [blame]
Harald Weltecbd18962019-03-03 19:02:38 +01001
2#include <stdint.h>
3#include <stdlib.h>
4#include <stdio.h>
5#include <errno.h>
6
7#include <pthread.h>
8
9#include <talloc.h>
10
11#include <osmocom/core/linuxlist.h>
Harald Welte91a0a4a2019-03-03 20:56:06 +010012#include <osmocom/core/utils.h>
Harald Weltecbd18962019-03-03 19:02:38 +010013
14#include "slotmap.h"
15
Harald Welte91a0a4a2019-03-03 20:56:06 +010016const struct value_string slot_map_state_name[] = {
17 { SLMAP_S_NEW, "NEW" },
18 { SLMAP_S_UNACKNOWLEDGED, "UNACKNOWLEDGED" },
19 { SLMAP_S_ACTIVE, "ACTIVE" },
Harald Welte2bf39e02019-03-06 16:05:20 +010020 { SLMAP_S_DELETE_REQ, "DELETE_REQ" },
Harald Welte91a0a4a2019-03-03 20:56:06 +010021 { SLMAP_S_DELETING, "DELETING" },
22 { 0, NULL }
23};
24
Harald Weltefaef8f02019-03-03 20:55:22 +010025const char *slotmap_name(char *buf, size_t buf_len, const struct slot_mapping *map)
26{
27 snprintf(buf, buf_len, "B(%u:%u) <-> C(%u:%u)",
28 map->bank.bank_id, map->bank.slot_nr, map->client.client_id, map->client.slot_nr);
29 return buf;
30}
31
32
Harald Weltecbd18962019-03-03 19:02:38 +010033/* thread-safe lookup of map by client:slot */
34struct slot_mapping *slotmap_by_client(struct slotmaps *maps, const struct client_slot *client)
35{
36 struct slot_mapping *map;
37
38 pthread_rwlock_rdlock(&maps->rwlock);
39 llist_for_each_entry(map, &maps->mappings, list) {
40 if (client_slot_equals(&map->client, client)) {
41 pthread_rwlock_unlock(&maps->rwlock);
42 return map;
43 }
44 }
45 pthread_rwlock_unlock(&maps->rwlock);
46 return NULL;
47}
48
49/* thread-safe lookup of map by bank:slot */
50struct slot_mapping *slotmap_by_bank(struct slotmaps *maps, const struct bank_slot *bank)
51{
52 struct slot_mapping *map;
53
54 pthread_rwlock_rdlock(&maps->rwlock);
55 llist_for_each_entry(map, &maps->mappings, list) {
56 if (bank_slot_equals(&map->bank, bank)) {
57 pthread_rwlock_unlock(&maps->rwlock);
58 return map;
59 }
60 }
61 pthread_rwlock_unlock(&maps->rwlock);
62 return NULL;
63
64}
65
66/* thread-safe creating of a new bank<->client map */
Harald Weltef4e75042019-03-03 21:54:52 +010067struct slot_mapping *slotmap_add(struct slotmaps *maps, const struct bank_slot *bank,
68 const struct client_slot *client)
Harald Weltecbd18962019-03-03 19:02:38 +010069{
70 struct slot_mapping *map;
Harald Weltefaef8f02019-03-03 20:55:22 +010071 char mapname[64];
Harald Weltecbd18962019-03-03 19:02:38 +010072
73 /* We assume a single thread (main thread) will ever update the mappings,
74 * and hence we don't have any races by first grabbing + releasing the read
75 * lock twice before grabbing the writelock below */
76
77 map = slotmap_by_bank(maps, bank);
78 if (map) {
79 fprintf(stderr, "BANKD %u:%u already in use, cannot add new map\n",
80 bank->bank_id, bank->slot_nr);
Harald Weltef4e75042019-03-03 21:54:52 +010081 return NULL;
Harald Weltecbd18962019-03-03 19:02:38 +010082 }
83
84 map = slotmap_by_client(maps, client);
85 if (map) {
86 fprintf(stderr, "CLIENT %u:%u already in use, cannot add new map\n",
87 client->client_id, client->slot_nr);
Harald Weltef4e75042019-03-03 21:54:52 +010088 return NULL;
Harald Weltecbd18962019-03-03 19:02:38 +010089 }
90
91 /* allocate new mapping and add to list of mappings */
92 map = talloc_zero(maps, struct slot_mapping);
93 if (!map)
Harald Weltef4e75042019-03-03 21:54:52 +010094 return NULL;
Harald Weltecbd18962019-03-03 19:02:38 +010095
Harald Welte91a0a4a2019-03-03 20:56:06 +010096 map->maps = maps;
Harald Weltecbd18962019-03-03 19:02:38 +010097 map->bank = *bank;
98 map->client = *client;
99
100 pthread_rwlock_wrlock(&maps->rwlock);
101 llist_add_tail(&map->list, &maps->mappings);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100102#ifdef REMSIM_SERVER
103 map->state = SLMAP_S_NEW;
104 INIT_LLIST_HEAD(&map->bank_list); /* to ensure llist_del() always succeeds */
105#endif
Harald Weltecbd18962019-03-03 19:02:38 +0100106 pthread_rwlock_unlock(&maps->rwlock);
107
Harald Weltefaef8f02019-03-03 20:55:22 +0100108 printf("Slot Map %s added\n", slotmap_name(mapname, sizeof(mapname), map));
Harald Weltecbd18962019-03-03 19:02:38 +0100109
Harald Weltef4e75042019-03-03 21:54:52 +0100110 return map;
Harald Weltecbd18962019-03-03 19:02:38 +0100111}
112
113/* thread-safe removal of a bank<->client map */
114void slotmap_del(struct slotmaps *maps, struct slot_mapping *map)
115{
Harald Weltefaef8f02019-03-03 20:55:22 +0100116 char mapname[64];
117
118 printf("Slot Map %s deleted\n", slotmap_name(mapname, sizeof(mapname), map));
Harald Weltecbd18962019-03-03 19:02:38 +0100119
120 pthread_rwlock_wrlock(&maps->rwlock);
121 llist_del(&map->list);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100122#ifdef REMSIM_SERVER
123 llist_del(&map->bank_list);
124#endif
Harald Weltecbd18962019-03-03 19:02:38 +0100125 pthread_rwlock_unlock(&maps->rwlock);
126
127 talloc_free(map);
128}
129
130struct slotmaps *slotmap_init(void *ctx)
131{
132 struct slotmaps *sm = talloc_zero(ctx, struct slotmaps);
133
134 INIT_LLIST_HEAD(&sm->mappings);
135 pthread_rwlock_init(&sm->rwlock, NULL);
136
137 return sm;
138}
Harald Welte91a0a4a2019-03-03 20:56:06 +0100139
140#ifdef REMSIM_SERVER
141
142void _slotmap_state_change(struct slot_mapping *map, enum slot_mapping_state new_state,
143 struct llist_head *new_bank_list)
144{
145 char mapname[64];
146
147 printf("Slot Map %s state change: %s -> %s\n", slotmap_name(mapname, sizeof(mapname), map),
148 get_value_string(slot_map_state_name, map->state),
149 get_value_string(slot_map_state_name, new_state));
150
151 map->state = new_state;
152#ifdef REMSIM_SERVER
153 llist_del(&map->bank_list);
154#endif
155 if (new_bank_list)
156 llist_add_tail(&map->bank_list, new_bank_list);
157#ifdef REMSIM_SERVER
158 else
159 INIT_LLIST_HEAD(&map->bank_list);
160#endif
161}
162
163
164void slotmap_state_change(struct slot_mapping *map, enum slot_mapping_state new_state,
165 struct llist_head *new_bank_list)
166{
167 pthread_rwlock_wrlock(&map->maps->rwlock);
168 _slotmap_state_change(map, new_state, new_bank_list);
169 pthread_rwlock_unlock(&map->maps->rwlock);
170}
171
172#endif