Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 1 | #pragma once |
| 2 | #include <stdint.h> |
| 3 | #include <stdbool.h> |
| 4 | #include <pthread.h> |
| 5 | #include <osmocom/core/linuxlist.h> |
| 6 | |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 7 | #define REMSIM_SERVER 1 |
| 8 | |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 9 | struct bank_slot { |
| 10 | uint16_t bank_id; |
| 11 | uint16_t slot_nr; |
| 12 | }; |
| 13 | |
| 14 | static inline bool bank_slot_equals(const struct bank_slot *a, const struct bank_slot *b) |
| 15 | { |
| 16 | if (a->bank_id == b->bank_id && a->slot_nr == b->slot_nr) |
| 17 | return true; |
| 18 | else |
| 19 | return false; |
| 20 | } |
| 21 | |
| 22 | struct client_slot { |
| 23 | uint16_t client_id; |
| 24 | uint16_t slot_nr; |
| 25 | }; |
| 26 | |
| 27 | static inline bool client_slot_equals(const struct client_slot *a, const struct client_slot *b) |
| 28 | { |
| 29 | if (a->client_id == b->client_id && a->slot_nr == b->slot_nr) |
| 30 | return true; |
| 31 | else |
| 32 | return false; |
| 33 | } |
| 34 | |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 35 | enum slot_mapping_state { |
| 36 | SLMAP_S_NEW, /* created; not yet sent to bankd */ |
| 37 | SLMAP_S_UNACKNOWLEDGED, /* created + sent to bankd but not yet acknowledge by bankd */ |
| 38 | SLMAP_S_ACTIVE, /* fully active map; acknowledged by bankd */ |
Harald Welte | 2bf39e0 | 2019-03-06 16:05:20 +0100 | [diff] [blame] | 39 | SLMAP_S_DELETE_REQ, /* fully active map; REST has requested deletion */ |
| 40 | SLMAP_S_DELETING, /* RSPRO has issued Remove to bankd, but bankd hasn't confirmed yet */ |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 41 | }; |
| 42 | extern const struct value_string slot_map_state_name[]; |
| 43 | static inline const char *slotmap_state_name(enum slot_mapping_state st) |
| 44 | { |
| 45 | return get_value_string(slot_map_state_name, st); |
| 46 | } |
| 47 | |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 48 | /* slot mappings are created / removed by the server */ |
| 49 | struct slot_mapping { |
| 50 | /* global lits of bankd slot mappings */ |
| 51 | struct llist_head list; |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 52 | struct slotmaps *maps; |
| 53 | |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 54 | /* slot on bank side */ |
| 55 | struct bank_slot bank; |
| 56 | /* slot on client side */ |
| 57 | struct client_slot client; |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 58 | |
| 59 | #ifdef REMSIM_SERVER |
| 60 | struct llist_head bank_list; |
| 61 | enum slot_mapping_state state; |
| 62 | #endif |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | /* collection of slot mappings */ |
| 66 | struct slotmaps { |
| 67 | struct llist_head mappings; |
| 68 | pthread_rwlock_t rwlock; |
| 69 | }; |
| 70 | |
Harald Welte | c865683 | 2019-03-06 16:05:42 +0100 | [diff] [blame] | 71 | uint32_t slotmap_get_id(const struct slot_mapping *map); |
| 72 | |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 73 | /* thread-safe lookup of map by client:slot */ |
| 74 | struct slot_mapping *slotmap_by_client(struct slotmaps *maps, const struct client_slot *client); |
| 75 | |
| 76 | /* thread-safe lookup of map by bank:slot */ |
| 77 | struct slot_mapping *slotmap_by_bank(struct slotmaps *maps, const struct bank_slot *bank); |
| 78 | |
| 79 | /* thread-safe creating of a new bank<->client map */ |
Harald Welte | f4e7504 | 2019-03-03 21:54:52 +0100 | [diff] [blame] | 80 | struct slot_mapping *slotmap_add(struct slotmaps *maps, const struct bank_slot *bank, const struct client_slot *client); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 81 | |
| 82 | /* thread-safe removal of a bank<->client map */ |
| 83 | void slotmap_del(struct slotmaps *maps, struct slot_mapping *map); |
Harald Welte | 294298c | 2019-03-07 10:08:25 +0100 | [diff] [blame] | 84 | void _slotmap_del(struct slotmaps *maps, struct slot_mapping *map); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 85 | |
| 86 | /* initialize the entire map collection */ |
| 87 | struct slotmaps *slotmap_init(void *ctx); |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 88 | |
Harald Welte | 13d0f8c | 2019-03-31 15:13:29 +0200 | [diff] [blame] | 89 | #ifdef SLOTMAP_DEBUG |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 90 | #define slotmaps_rdlock(maps) do { \ |
| 91 | printf("%s:%u = slotmap_rdlock()\n", __FILE__, __LINE__); \ |
| 92 | pthread_rwlock_rdlock(&(maps)->rwlock); \ |
| 93 | } while (0) |
| 94 | |
| 95 | #define slotmaps_wrlock(maps) do { \ |
| 96 | printf("%s:%u = slotmap_wrlock()\n", __FILE__, __LINE__); \ |
| 97 | pthread_rwlock_wrlock(&(maps)->rwlock); \ |
| 98 | } while (0) |
| 99 | |
| 100 | #define slotmaps_unlock(maps) do { \ |
| 101 | printf("%s:%u = slotmap_unlock()\n", __FILE__, __LINE__); \ |
| 102 | pthread_rwlock_unlock(&(maps)->rwlock); \ |
| 103 | } while (0) |
Harald Welte | 13d0f8c | 2019-03-31 15:13:29 +0200 | [diff] [blame] | 104 | #else |
| 105 | #define slotmaps_rdlock(maps) pthread_rwlock_rdlock(&(maps)->rwlock) |
| 106 | #define slotmaps_wrlock(maps) pthread_rwlock_wrlock(&(maps)->rwlock) |
| 107 | #define slotmaps_unlock(maps) pthread_rwlock_unlock(&(maps)->rwlock) |
| 108 | #endif |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 109 | |
| 110 | |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 111 | #ifdef REMSIM_SERVER |
Harald Welte | 4b676bc | 2019-03-07 21:01:38 +0100 | [diff] [blame] | 112 | void _Slotmap_state_change(struct slot_mapping *map, enum slot_mapping_state new_state, |
| 113 | struct llist_head *new_bank_list, const char *file, int line); |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 114 | /* thread-safe way to change the state of given slot map */ |
Harald Welte | 4b676bc | 2019-03-07 21:01:38 +0100 | [diff] [blame] | 115 | void Slotmap_state_change(struct slot_mapping *map, enum slot_mapping_state new_state, |
| 116 | struct llist_head *new_bank_list, const char *file, int line); |
| 117 | #define _slotmap_state_change(map, state, list) \ |
| 118 | _Slotmap_state_change(map, state, list, __FILE__, __LINE__) |
| 119 | #define slotmap_state_change(map, state, list) \ |
| 120 | Slotmap_state_change(map, state, list, __FILE__, __LINE__) |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 121 | #endif |