blob: 703dd028c5f88736aa98626585cc5e2b087f2892 [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 */
118void slotmap_del(struct slotmaps *maps, struct slot_mapping *map)
119{
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
124 pthread_rwlock_wrlock(&maps->rwlock);
125 llist_del(&map->list);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100126#ifdef REMSIM_SERVER
127 llist_del(&map->bank_list);
128#endif
Harald Weltecbd18962019-03-03 19:02:38 +0100129 pthread_rwlock_unlock(&maps->rwlock);
130
131 talloc_free(map);
132}
133
134struct slotmaps *slotmap_init(void *ctx)
135{
136 struct slotmaps *sm = talloc_zero(ctx, struct slotmaps);
137
138 INIT_LLIST_HEAD(&sm->mappings);
139 pthread_rwlock_init(&sm->rwlock, NULL);
140
141 return sm;
142}
Harald Welte91a0a4a2019-03-03 20:56:06 +0100143
144#ifdef REMSIM_SERVER
145
146void _slotmap_state_change(struct slot_mapping *map, enum slot_mapping_state new_state,
147 struct llist_head *new_bank_list)
148{
149 char mapname[64];
150
151 printf("Slot Map %s state change: %s -> %s\n", slotmap_name(mapname, sizeof(mapname), map),
152 get_value_string(slot_map_state_name, map->state),
153 get_value_string(slot_map_state_name, new_state));
154
155 map->state = new_state;
156#ifdef REMSIM_SERVER
157 llist_del(&map->bank_list);
158#endif
159 if (new_bank_list)
160 llist_add_tail(&map->bank_list, new_bank_list);
161#ifdef REMSIM_SERVER
162 else
163 INIT_LLIST_HEAD(&map->bank_list);
164#endif
165}
166
167
168void slotmap_state_change(struct slot_mapping *map, enum slot_mapping_state new_state,
169 struct llist_head *new_bank_list)
170{
171 pthread_rwlock_wrlock(&map->maps->rwlock);
172 _slotmap_state_change(map, new_state, new_bank_list);
173 pthread_rwlock_unlock(&map->maps->rwlock);
174}
175
176#endif