blob: 3d07c8ddd1ce4f88670d10be693f7667bafc55d5 [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
Harald Weltef34fb792019-12-04 19:51:32 +010086/* thread-safe removal of all bank<->client maps */
87void slotmap_del_all(struct slotmaps *maps);
88
Harald Weltecbd18962019-03-03 19:02:38 +010089/* initialize the entire map collection */
90struct slotmaps *slotmap_init(void *ctx);
Harald Welte91a0a4a2019-03-03 20:56:06 +010091
Harald Welte13d0f8c2019-03-31 15:13:29 +020092#ifdef SLOTMAP_DEBUG
Harald Welte648f4e32019-03-07 21:14:47 +010093#define slotmaps_rdlock(maps) do { \
94 printf("%s:%u = slotmap_rdlock()\n", __FILE__, __LINE__); \
95 pthread_rwlock_rdlock(&(maps)->rwlock); \
96} while (0)
97
98#define slotmaps_wrlock(maps) do { \
99 printf("%s:%u = slotmap_wrlock()\n", __FILE__, __LINE__); \
100 pthread_rwlock_wrlock(&(maps)->rwlock); \
101} while (0)
102
103#define slotmaps_unlock(maps) do { \
104 printf("%s:%u = slotmap_unlock()\n", __FILE__, __LINE__); \
105 pthread_rwlock_unlock(&(maps)->rwlock); \
106} while (0)
Harald Welte13d0f8c2019-03-31 15:13:29 +0200107#else
108#define slotmaps_rdlock(maps) pthread_rwlock_rdlock(&(maps)->rwlock)
109#define slotmaps_wrlock(maps) pthread_rwlock_wrlock(&(maps)->rwlock)
110#define slotmaps_unlock(maps) pthread_rwlock_unlock(&(maps)->rwlock)
111#endif
Harald Welte648f4e32019-03-07 21:14:47 +0100112
113
Harald Welte91a0a4a2019-03-03 20:56:06 +0100114#ifdef REMSIM_SERVER
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);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100117/* thread-safe way to change the state of given slot map */
Harald Welte4b676bc2019-03-07 21:01:38 +0100118void Slotmap_state_change(struct slot_mapping *map, enum slot_mapping_state new_state,
119 struct llist_head *new_bank_list, const char *file, int line);
120#define _slotmap_state_change(map, state, list) \
121 _Slotmap_state_change(map, state, list, __FILE__, __LINE__)
122#define slotmap_state_change(map, state, list) \
123 Slotmap_state_change(map, state, list, __FILE__, __LINE__)
Harald Welte91a0a4a2019-03-03 20:56:06 +0100124#endif