Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 1 | |
| 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 Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 12 | #include <osmocom/core/utils.h> |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 13 | |
| 14 | #include "slotmap.h" |
| 15 | |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 16 | const 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 Welte | faef8f0 | 2019-03-03 20:55:22 +0100 | [diff] [blame] | 24 | const 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 Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 32 | /* thread-safe lookup of map by client:slot */ |
| 33 | struct 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 */ |
| 49 | struct 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 Welte | f4e7504 | 2019-03-03 21:54:52 +0100 | [diff] [blame^] | 66 | struct slot_mapping *slotmap_add(struct slotmaps *maps, const struct bank_slot *bank, |
| 67 | const struct client_slot *client) |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 68 | { |
| 69 | struct slot_mapping *map; |
Harald Welte | faef8f0 | 2019-03-03 20:55:22 +0100 | [diff] [blame] | 70 | char mapname[64]; |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 71 | |
| 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 Welte | f4e7504 | 2019-03-03 21:54:52 +0100 | [diff] [blame^] | 80 | return NULL; |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 81 | } |
| 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 Welte | f4e7504 | 2019-03-03 21:54:52 +0100 | [diff] [blame^] | 87 | return NULL; |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /* allocate new mapping and add to list of mappings */ |
| 91 | map = talloc_zero(maps, struct slot_mapping); |
| 92 | if (!map) |
Harald Welte | f4e7504 | 2019-03-03 21:54:52 +0100 | [diff] [blame^] | 93 | return NULL; |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 94 | |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 95 | map->maps = maps; |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 96 | map->bank = *bank; |
| 97 | map->client = *client; |
| 98 | |
| 99 | pthread_rwlock_wrlock(&maps->rwlock); |
| 100 | llist_add_tail(&map->list, &maps->mappings); |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 101 | #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 Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 105 | pthread_rwlock_unlock(&maps->rwlock); |
| 106 | |
Harald Welte | faef8f0 | 2019-03-03 20:55:22 +0100 | [diff] [blame] | 107 | printf("Slot Map %s added\n", slotmap_name(mapname, sizeof(mapname), map)); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 108 | |
Harald Welte | f4e7504 | 2019-03-03 21:54:52 +0100 | [diff] [blame^] | 109 | return map; |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /* thread-safe removal of a bank<->client map */ |
| 113 | void slotmap_del(struct slotmaps *maps, struct slot_mapping *map) |
| 114 | { |
Harald Welte | faef8f0 | 2019-03-03 20:55:22 +0100 | [diff] [blame] | 115 | char mapname[64]; |
| 116 | |
| 117 | printf("Slot Map %s deleted\n", slotmap_name(mapname, sizeof(mapname), map)); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 118 | |
| 119 | pthread_rwlock_wrlock(&maps->rwlock); |
| 120 | llist_del(&map->list); |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 121 | #ifdef REMSIM_SERVER |
| 122 | llist_del(&map->bank_list); |
| 123 | #endif |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 124 | pthread_rwlock_unlock(&maps->rwlock); |
| 125 | |
| 126 | talloc_free(map); |
| 127 | } |
| 128 | |
| 129 | struct 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 Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 138 | |
| 139 | #ifdef REMSIM_SERVER |
| 140 | |
| 141 | void _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 | |
| 163 | void 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 |