blob: 12d7777be5387184c1518f19c7c8920ff51cc51f [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" },
20 { SLMAP_S_DELETING, "DELETING" },
21 { 0, NULL }
22};
23
Harald Weltefaef8f02019-03-03 20:55:22 +010024const char *slotmap_name(char *buf, size_t buf_len, const struct slot_mapping *map)
25{
26 snprintf(buf, buf_len, "B(%u:%u) <-> C(%u:%u)",
27 map->bank.bank_id, map->bank.slot_nr, map->client.client_id, map->client.slot_nr);
28 return buf;
29}
30
31
Harald Weltecbd18962019-03-03 19:02:38 +010032/* thread-safe lookup of map by client:slot */
33struct slot_mapping *slotmap_by_client(struct slotmaps *maps, const struct client_slot *client)
34{
35 struct slot_mapping *map;
36
37 pthread_rwlock_rdlock(&maps->rwlock);
38 llist_for_each_entry(map, &maps->mappings, list) {
39 if (client_slot_equals(&map->client, client)) {
40 pthread_rwlock_unlock(&maps->rwlock);
41 return map;
42 }
43 }
44 pthread_rwlock_unlock(&maps->rwlock);
45 return NULL;
46}
47
48/* thread-safe lookup of map by bank:slot */
49struct slot_mapping *slotmap_by_bank(struct slotmaps *maps, const struct bank_slot *bank)
50{
51 struct slot_mapping *map;
52
53 pthread_rwlock_rdlock(&maps->rwlock);
54 llist_for_each_entry(map, &maps->mappings, list) {
55 if (bank_slot_equals(&map->bank, bank)) {
56 pthread_rwlock_unlock(&maps->rwlock);
57 return map;
58 }
59 }
60 pthread_rwlock_unlock(&maps->rwlock);
61 return NULL;
62
63}
64
65/* thread-safe creating of a new bank<->client map */
Harald Weltef4e75042019-03-03 21:54:52 +010066struct slot_mapping *slotmap_add(struct slotmaps *maps, const struct bank_slot *bank,
67 const struct client_slot *client)
Harald Weltecbd18962019-03-03 19:02:38 +010068{
69 struct slot_mapping *map;
Harald Weltefaef8f02019-03-03 20:55:22 +010070 char mapname[64];
Harald Weltecbd18962019-03-03 19:02:38 +010071
72 /* We assume a single thread (main thread) will ever update the mappings,
73 * and hence we don't have any races by first grabbing + releasing the read
74 * lock twice before grabbing the writelock below */
75
76 map = slotmap_by_bank(maps, bank);
77 if (map) {
78 fprintf(stderr, "BANKD %u:%u already in use, cannot add new map\n",
79 bank->bank_id, bank->slot_nr);
Harald Weltef4e75042019-03-03 21:54:52 +010080 return NULL;
Harald Weltecbd18962019-03-03 19:02:38 +010081 }
82
83 map = slotmap_by_client(maps, client);
84 if (map) {
85 fprintf(stderr, "CLIENT %u:%u already in use, cannot add new map\n",
86 client->client_id, client->slot_nr);
Harald Weltef4e75042019-03-03 21:54:52 +010087 return NULL;
Harald Weltecbd18962019-03-03 19:02:38 +010088 }
89
90 /* allocate new mapping and add to list of mappings */
91 map = talloc_zero(maps, struct slot_mapping);
92 if (!map)
Harald Weltef4e75042019-03-03 21:54:52 +010093 return NULL;
Harald Weltecbd18962019-03-03 19:02:38 +010094
Harald Welte91a0a4a2019-03-03 20:56:06 +010095 map->maps = maps;
Harald Weltecbd18962019-03-03 19:02:38 +010096 map->bank = *bank;
97 map->client = *client;
98
99 pthread_rwlock_wrlock(&maps->rwlock);
100 llist_add_tail(&map->list, &maps->mappings);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100101#ifdef REMSIM_SERVER
102 map->state = SLMAP_S_NEW;
103 INIT_LLIST_HEAD(&map->bank_list); /* to ensure llist_del() always succeeds */
104#endif
Harald Weltecbd18962019-03-03 19:02:38 +0100105 pthread_rwlock_unlock(&maps->rwlock);
106
Harald Weltefaef8f02019-03-03 20:55:22 +0100107 printf("Slot Map %s added\n", slotmap_name(mapname, sizeof(mapname), map));
Harald Weltecbd18962019-03-03 19:02:38 +0100108
Harald Weltef4e75042019-03-03 21:54:52 +0100109 return map;
Harald Weltecbd18962019-03-03 19:02:38 +0100110}
111
112/* thread-safe removal of a bank<->client map */
113void slotmap_del(struct slotmaps *maps, struct slot_mapping *map)
114{
Harald Weltefaef8f02019-03-03 20:55:22 +0100115 char mapname[64];
116
117 printf("Slot Map %s deleted\n", slotmap_name(mapname, sizeof(mapname), map));
Harald Weltecbd18962019-03-03 19:02:38 +0100118
119 pthread_rwlock_wrlock(&maps->rwlock);
120 llist_del(&map->list);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100121#ifdef REMSIM_SERVER
122 llist_del(&map->bank_list);
123#endif
Harald Weltecbd18962019-03-03 19:02:38 +0100124 pthread_rwlock_unlock(&maps->rwlock);
125
126 talloc_free(map);
127}
128
129struct slotmaps *slotmap_init(void *ctx)
130{
131 struct slotmaps *sm = talloc_zero(ctx, struct slotmaps);
132
133 INIT_LLIST_HEAD(&sm->mappings);
134 pthread_rwlock_init(&sm->rwlock, NULL);
135
136 return sm;
137}
Harald Welte91a0a4a2019-03-03 20:56:06 +0100138
139#ifdef REMSIM_SERVER
140
141void _slotmap_state_change(struct slot_mapping *map, enum slot_mapping_state new_state,
142 struct llist_head *new_bank_list)
143{
144 char mapname[64];
145
146 printf("Slot Map %s state change: %s -> %s\n", slotmap_name(mapname, sizeof(mapname), map),
147 get_value_string(slot_map_state_name, map->state),
148 get_value_string(slot_map_state_name, new_state));
149
150 map->state = new_state;
151#ifdef REMSIM_SERVER
152 llist_del(&map->bank_list);
153#endif
154 if (new_bank_list)
155 llist_add_tail(&map->bank_list, new_bank_list);
156#ifdef REMSIM_SERVER
157 else
158 INIT_LLIST_HEAD(&map->bank_list);
159#endif
160}
161
162
163void slotmap_state_change(struct slot_mapping *map, enum slot_mapping_state new_state,
164 struct llist_head *new_bank_list)
165{
166 pthread_rwlock_wrlock(&map->maps->rwlock);
167 _slotmap_state_change(map, new_state, new_bank_list);
168 pthread_rwlock_unlock(&map->maps->rwlock);
169}
170
171#endif