blob: bf18e04c78a5e30993d8c35a40e6eae28f569f62 [file] [log] [blame]
Harald Weltecbd18962019-03-03 19:02:38 +01001#pragma once
2#include <stdint.h>
3#include <stdbool.h>
4#include <pthread.h>
5#include <osmocom/core/linuxlist.h>
6
Harald Welte91a0a4a2019-03-03 20:56:06 +01007#define REMSIM_SERVER 1
8
Harald Weltecbd18962019-03-03 19:02:38 +01009struct bank_slot {
10 uint16_t bank_id;
11 uint16_t slot_nr;
12};
13
14static 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
22struct client_slot {
23 uint16_t client_id;
24 uint16_t slot_nr;
25};
26
27static 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 Welte91a0a4a2019-03-03 20:56:06 +010035enum 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 Welte2bf39e02019-03-06 16:05:20 +010039 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 Welte91a0a4a2019-03-03 20:56:06 +010041};
42extern const struct value_string slot_map_state_name[];
43static 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 Weltecbd18962019-03-03 19:02:38 +010048/* slot mappings are created / removed by the server */
49struct slot_mapping {
50 /* global lits of bankd slot mappings */
51 struct llist_head list;
Harald Welte91a0a4a2019-03-03 20:56:06 +010052 struct slotmaps *maps;
53
Harald Weltecbd18962019-03-03 19:02:38 +010054 /* slot on bank side */
55 struct bank_slot bank;
56 /* slot on client side */
57 struct client_slot client;
Harald Welte91a0a4a2019-03-03 20:56:06 +010058
59#ifdef REMSIM_SERVER
60 struct llist_head bank_list;
61 enum slot_mapping_state state;
62#endif
Harald Weltecbd18962019-03-03 19:02:38 +010063};
64
65/* collection of slot mappings */
66struct slotmaps {
67 struct llist_head mappings;
68 pthread_rwlock_t rwlock;
69};
70
Harald Weltec8656832019-03-06 16:05:42 +010071uint32_t slotmap_get_id(const struct slot_mapping *map);
72
Harald Weltecbd18962019-03-03 19:02:38 +010073/* thread-safe lookup of map by client:slot */
74struct slot_mapping *slotmap_by_client(struct slotmaps *maps, const struct client_slot *client);
75
76/* thread-safe lookup of map by bank:slot */
77struct 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 Weltef4e75042019-03-03 21:54:52 +010080struct slot_mapping *slotmap_add(struct slotmaps *maps, const struct bank_slot *bank, const struct client_slot *client);
Harald Weltecbd18962019-03-03 19:02:38 +010081
82/* thread-safe removal of a bank<->client map */
83void slotmap_del(struct slotmaps *maps, struct slot_mapping *map);
Harald Welte294298c2019-03-07 10:08:25 +010084void _slotmap_del(struct slotmaps *maps, struct slot_mapping *map);
Harald Weltecbd18962019-03-03 19:02:38 +010085
86/* initialize the entire map collection */
87struct slotmaps *slotmap_init(void *ctx);
Harald Welte91a0a4a2019-03-03 20:56:06 +010088
Harald Welte13d0f8c2019-03-31 15:13:29 +020089#ifdef SLOTMAP_DEBUG
Harald Welte648f4e32019-03-07 21:14:47 +010090#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 Welte13d0f8c2019-03-31 15:13:29 +0200104#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 Welte648f4e32019-03-07 21:14:47 +0100109
110
Harald Welte91a0a4a2019-03-03 20:56:06 +0100111#ifdef REMSIM_SERVER
Harald Welte4b676bc2019-03-07 21:01:38 +0100112void _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 Welte91a0a4a2019-03-03 20:56:06 +0100114/* thread-safe way to change the state of given slot map */
Harald Welte4b676bc2019-03-07 21:01:38 +0100115void 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 Welte91a0a4a2019-03-03 20:56:06 +0100121#endif