blob: 924452b63ebc62bc0cc74ac5f6c2b8aba10ffeb5 [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 */
66int slotmap_add(struct slotmaps *maps, const struct bank_slot *bank, const struct client_slot *client)
67{
68 struct slot_mapping *map;
Harald Weltefaef8f02019-03-03 20:55:22 +010069 char mapname[64];
Harald Weltecbd18962019-03-03 19:02:38 +010070
71 /* We assume a single thread (main thread) will ever update the mappings,
72 * and hence we don't have any races by first grabbing + releasing the read
73 * lock twice before grabbing the writelock below */
74
75 map = slotmap_by_bank(maps, bank);
76 if (map) {
77 fprintf(stderr, "BANKD %u:%u already in use, cannot add new map\n",
78 bank->bank_id, bank->slot_nr);
79 return -EBUSY;
80 }
81
82 map = slotmap_by_client(maps, client);
83 if (map) {
84 fprintf(stderr, "CLIENT %u:%u already in use, cannot add new map\n",
85 client->client_id, client->slot_nr);
86 return -EBUSY;
87 }
88
89 /* allocate new mapping and add to list of mappings */
90 map = talloc_zero(maps, struct slot_mapping);
91 if (!map)
92 return -ENOMEM;
93
Harald Welte91a0a4a2019-03-03 20:56:06 +010094 map->maps = maps;
Harald Weltecbd18962019-03-03 19:02:38 +010095 map->bank = *bank;
96 map->client = *client;
97
98 pthread_rwlock_wrlock(&maps->rwlock);
99 llist_add_tail(&map->list, &maps->mappings);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100100#ifdef REMSIM_SERVER
101 map->state = SLMAP_S_NEW;
102 INIT_LLIST_HEAD(&map->bank_list); /* to ensure llist_del() always succeeds */
103#endif
Harald Weltecbd18962019-03-03 19:02:38 +0100104 pthread_rwlock_unlock(&maps->rwlock);
105
Harald Weltefaef8f02019-03-03 20:55:22 +0100106 printf("Slot Map %s added\n", slotmap_name(mapname, sizeof(mapname), map));
Harald Weltecbd18962019-03-03 19:02:38 +0100107
108 return 0;
109}
110
111/* thread-safe removal of a bank<->client map */
112void slotmap_del(struct slotmaps *maps, struct slot_mapping *map)
113{
Harald Weltefaef8f02019-03-03 20:55:22 +0100114 char mapname[64];
115
116 printf("Slot Map %s deleted\n", slotmap_name(mapname, sizeof(mapname), map));
Harald Weltecbd18962019-03-03 19:02:38 +0100117
118 pthread_rwlock_wrlock(&maps->rwlock);
119 llist_del(&map->list);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100120#ifdef REMSIM_SERVER
121 llist_del(&map->bank_list);
122#endif
Harald Weltecbd18962019-03-03 19:02:38 +0100123 pthread_rwlock_unlock(&maps->rwlock);
124
125 talloc_free(map);
126}
127
128struct slotmaps *slotmap_init(void *ctx)
129{
130 struct slotmaps *sm = talloc_zero(ctx, struct slotmaps);
131
132 INIT_LLIST_HEAD(&sm->mappings);
133 pthread_rwlock_init(&sm->rwlock, NULL);
134
135 return sm;
136}
Harald Welte91a0a4a2019-03-03 20:56:06 +0100137
138#ifdef REMSIM_SERVER
139
140void _slotmap_state_change(struct slot_mapping *map, enum slot_mapping_state new_state,
141 struct llist_head *new_bank_list)
142{
143 char mapname[64];
144
145 printf("Slot Map %s state change: %s -> %s\n", slotmap_name(mapname, sizeof(mapname), map),
146 get_value_string(slot_map_state_name, map->state),
147 get_value_string(slot_map_state_name, new_state));
148
149 map->state = new_state;
150#ifdef REMSIM_SERVER
151 llist_del(&map->bank_list);
152#endif
153 if (new_bank_list)
154 llist_add_tail(&map->bank_list, new_bank_list);
155#ifdef REMSIM_SERVER
156 else
157 INIT_LLIST_HEAD(&map->bank_list);
158#endif
159}
160
161
162void slotmap_state_change(struct slot_mapping *map, enum slot_mapping_state new_state,
163 struct llist_head *new_bank_list)
164{
165 pthread_rwlock_wrlock(&map->maps->rwlock);
166 _slotmap_state_change(map, new_state, new_bank_list);
167 pthread_rwlock_unlock(&map->maps->rwlock);
168}
169
170#endif