blob: aa036388c82e541b1b2d379525e4cbbfa4830340 [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
Harald Weltec8656832019-03-06 16:05:42 +010032uint32_t slotmap_get_id(const struct slot_mapping *map)
33{
34 return (map->bank.bank_id << 16) | map->bank.slot_nr;
35}
Harald Weltefaef8f02019-03-03 20:55:22 +010036
Harald Weltecbd18962019-03-03 19:02:38 +010037/* thread-safe lookup of map by client:slot */
38struct slot_mapping *slotmap_by_client(struct slotmaps *maps, const struct client_slot *client)
39{
40 struct slot_mapping *map;
41
42 pthread_rwlock_rdlock(&maps->rwlock);
43 llist_for_each_entry(map, &maps->mappings, list) {
44 if (client_slot_equals(&map->client, client)) {
45 pthread_rwlock_unlock(&maps->rwlock);
46 return map;
47 }
48 }
49 pthread_rwlock_unlock(&maps->rwlock);
50 return NULL;
51}
52
53/* thread-safe lookup of map by bank:slot */
54struct slot_mapping *slotmap_by_bank(struct slotmaps *maps, const struct bank_slot *bank)
55{
56 struct slot_mapping *map;
57
58 pthread_rwlock_rdlock(&maps->rwlock);
59 llist_for_each_entry(map, &maps->mappings, list) {
60 if (bank_slot_equals(&map->bank, bank)) {
61 pthread_rwlock_unlock(&maps->rwlock);
62 return map;
63 }
64 }
65 pthread_rwlock_unlock(&maps->rwlock);
66 return NULL;
67
68}
69
70/* thread-safe creating of a new bank<->client map */
Harald Weltef4e75042019-03-03 21:54:52 +010071struct slot_mapping *slotmap_add(struct slotmaps *maps, const struct bank_slot *bank,
72 const struct client_slot *client)
Harald Weltecbd18962019-03-03 19:02:38 +010073{
74 struct slot_mapping *map;
Harald Weltefaef8f02019-03-03 20:55:22 +010075 char mapname[64];
Harald Weltecbd18962019-03-03 19:02:38 +010076
77 /* We assume a single thread (main thread) will ever update the mappings,
78 * and hence we don't have any races by first grabbing + releasing the read
79 * lock twice before grabbing the writelock below */
80
81 map = slotmap_by_bank(maps, bank);
82 if (map) {
83 fprintf(stderr, "BANKD %u:%u already in use, cannot add new map\n",
84 bank->bank_id, bank->slot_nr);
Harald Weltef4e75042019-03-03 21:54:52 +010085 return NULL;
Harald Weltecbd18962019-03-03 19:02:38 +010086 }
87
88 map = slotmap_by_client(maps, client);
89 if (map) {
90 fprintf(stderr, "CLIENT %u:%u already in use, cannot add new map\n",
91 client->client_id, client->slot_nr);
Harald Weltef4e75042019-03-03 21:54:52 +010092 return NULL;
Harald Weltecbd18962019-03-03 19:02:38 +010093 }
94
95 /* allocate new mapping and add to list of mappings */
96 map = talloc_zero(maps, struct slot_mapping);
97 if (!map)
Harald Weltef4e75042019-03-03 21:54:52 +010098 return NULL;
Harald Weltecbd18962019-03-03 19:02:38 +010099
Harald Welte91a0a4a2019-03-03 20:56:06 +0100100 map->maps = maps;
Harald Weltecbd18962019-03-03 19:02:38 +0100101 map->bank = *bank;
102 map->client = *client;
103
104 pthread_rwlock_wrlock(&maps->rwlock);
105 llist_add_tail(&map->list, &maps->mappings);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100106#ifdef REMSIM_SERVER
107 map->state = SLMAP_S_NEW;
108 INIT_LLIST_HEAD(&map->bank_list); /* to ensure llist_del() always succeeds */
109#endif
Harald Weltecbd18962019-03-03 19:02:38 +0100110 pthread_rwlock_unlock(&maps->rwlock);
111
Harald Weltefaef8f02019-03-03 20:55:22 +0100112 printf("Slot Map %s added\n", slotmap_name(mapname, sizeof(mapname), map));
Harald Weltecbd18962019-03-03 19:02:38 +0100113
Harald Weltef4e75042019-03-03 21:54:52 +0100114 return map;
Harald Weltecbd18962019-03-03 19:02:38 +0100115}
116
117/* thread-safe removal of a bank<->client map */
Harald Welte294298c2019-03-07 10:08:25 +0100118void _slotmap_del(struct slotmaps *maps, struct slot_mapping *map)
Harald Weltecbd18962019-03-03 19:02:38 +0100119{
Harald Weltefaef8f02019-03-03 20:55:22 +0100120 char mapname[64];
121
122 printf("Slot Map %s deleted\n", slotmap_name(mapname, sizeof(mapname), map));
Harald Weltecbd18962019-03-03 19:02:38 +0100123
Harald Weltecbd18962019-03-03 19:02:38 +0100124 llist_del(&map->list);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100125#ifdef REMSIM_SERVER
126 llist_del(&map->bank_list);
127#endif
Harald Weltecbd18962019-03-03 19:02:38 +0100128
129 talloc_free(map);
130}
Harald Welte294298c2019-03-07 10:08:25 +0100131/* thread-safe removal of a bank<->client map */
132void slotmap_del(struct slotmaps *maps, struct slot_mapping *map)
133{
134 pthread_rwlock_wrlock(&maps->rwlock);
135 _slotmap_del(maps, map);
136 pthread_rwlock_unlock(&maps->rwlock);
137}
Harald Weltecbd18962019-03-03 19:02:38 +0100138
139struct slotmaps *slotmap_init(void *ctx)
140{
141 struct slotmaps *sm = talloc_zero(ctx, struct slotmaps);
142
143 INIT_LLIST_HEAD(&sm->mappings);
144 pthread_rwlock_init(&sm->rwlock, NULL);
145
146 return sm;
147}
Harald Welte91a0a4a2019-03-03 20:56:06 +0100148
149#ifdef REMSIM_SERVER
150
151void _slotmap_state_change(struct slot_mapping *map, enum slot_mapping_state new_state,
152 struct llist_head *new_bank_list)
153{
154 char mapname[64];
155
156 printf("Slot Map %s state change: %s -> %s\n", slotmap_name(mapname, sizeof(mapname), map),
157 get_value_string(slot_map_state_name, map->state),
158 get_value_string(slot_map_state_name, new_state));
159
160 map->state = new_state;
161#ifdef REMSIM_SERVER
162 llist_del(&map->bank_list);
163#endif
164 if (new_bank_list)
165 llist_add_tail(&map->bank_list, new_bank_list);
166#ifdef REMSIM_SERVER
167 else
168 INIT_LLIST_HEAD(&map->bank_list);
169#endif
170}
171
172
173void slotmap_state_change(struct slot_mapping *map, enum slot_mapping_state new_state,
174 struct llist_head *new_bank_list)
175{
176 pthread_rwlock_wrlock(&map->maps->rwlock);
177 _slotmap_state_change(map, new_state, new_bank_list);
178 pthread_rwlock_unlock(&map->maps->rwlock);
179}
180
181#endif