aboutsummaryrefslogtreecommitdiffstats
path: root/src/osmo-bsc/bssmap_reset.c
blob: 98f6b65b0b9261f32a1670a6d967a2efed16a474 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* (C) 2020 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
 * All Rights Reserved
 *
 * Authors: Philipp Maier, Neels Hofmeyr
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <osmocom/core/fsm.h>
#include <osmocom/core/tdef.h>
#include <osmocom/bsc/debug.h>
#include <osmocom/bsc/bssmap_reset.h>
#include <osmocom/bsc/gsm_data.h>

static struct osmo_fsm bssmap_reset_fsm;

enum bssmap_reset_fsm_state {
	BSSMAP_RESET_ST_DISC,
	BSSMAP_RESET_ST_CONN,
};

static const struct value_string bssmap_reset_fsm_event_names[] = {
	OSMO_VALUE_STRING(BSSMAP_RESET_EV_RX_RESET_ACK),
	OSMO_VALUE_STRING(BSSMAP_RESET_EV_CONN_CFM_FAILURE),
	OSMO_VALUE_STRING(BSSMAP_RESET_EV_CONN_CFM_SUCCESS),
	{}
};

static const struct osmo_tdef_state_timeout bssmap_reset_timeouts[32] = {
	[BSSMAP_RESET_ST_DISC] = { .T = 4 },
};

#define bssmap_reset_fsm_state_chg(FI, STATE) \
	osmo_tdef_fsm_inst_state_chg(FI, STATE, \
				     bssmap_reset_timeouts, \
				     (bsc_gsmnet)->T_defs, \
				     5)

struct bssmap_reset *bssmap_reset_alloc(void *ctx, const char *label, const struct bssmap_reset_cfg *cfg)
{
	struct bssmap_reset *bssmap_reset;
	struct osmo_fsm_inst *fi;

	fi = osmo_fsm_inst_alloc(&bssmap_reset_fsm, ctx, NULL, LOGL_DEBUG, label);
	OSMO_ASSERT(fi);

	bssmap_reset = talloc_zero(fi, struct bssmap_reset);
	OSMO_ASSERT(bssmap_reset);
	*bssmap_reset = (struct bssmap_reset){
		.fi = fi,
		.cfg = *cfg,
	};
	fi->priv = bssmap_reset;

	/* Immediately (1ms) kick off reset sending mechanism */
	osmo_fsm_inst_state_chg_ms(fi, BSSMAP_RESET_ST_DISC, 1, 0);
	return bssmap_reset;
}

static void link_up(struct bssmap_reset *bssmap_reset)
{
	LOGPFSML(bssmap_reset->fi, LOGL_NOTICE, "link up\n");
	bssmap_reset->conn_cfm_failures = 0;
	if (bssmap_reset->cfg.ops.link_up)
		bssmap_reset->cfg.ops.link_up(bssmap_reset->cfg.data);
}

static void link_lost(struct bssmap_reset *bssmap_reset)
{
	LOGPFSML(bssmap_reset->fi, LOGL_NOTICE, "link lost\n");
	if (bssmap_reset->cfg.ops.link_lost)
		bssmap_reset->cfg.ops.link_lost(bssmap_reset->cfg.data);
}

static void tx_reset(struct bssmap_reset *bssmap_reset)
{
	if (bssmap_reset->cfg.ops.tx_reset)
		bssmap_reset->cfg.ops.tx_reset(bssmap_reset->cfg.data);
}

static void bssmap_reset_disc_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	struct bssmap_reset *bssmap_reset = (struct bssmap_reset*)fi->priv;
	if (prev_state == BSSMAP_RESET_ST_CONN)
		link_lost(bssmap_reset);
}

static void bssmap_reset_disc_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	bssmap_reset_fsm_state_chg(fi, BSSMAP_RESET_ST_CONN);
}

static void bssmap_reset_conn_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
{
	struct bssmap_reset *bssmap_reset = (struct bssmap_reset*)fi->priv;
	if (prev_state != BSSMAP_RESET_ST_CONN)
		link_up(bssmap_reset);
}

static void bssmap_reset_conn_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
	struct bssmap_reset *bssmap_reset = (struct bssmap_reset*)fi->priv;

	switch (event) {

	case BSSMAP_RESET_EV_RX_RESET_ACK:
		LOGPFSML(fi, LOGL_INFO, "Ignoring duplicate RESET ACK\n");
		break;

	case BSSMAP_RESET_EV_CONN_CFM_FAILURE:
		bssmap_reset->conn_cfm_failures++;
		if (bssmap_reset->conn_cfm_failures > bssmap_reset->cfg.conn_cfm_failure_threshold)
			bssmap_reset_fsm_state_chg(fi, BSSMAP_RESET_ST_DISC);
		break;

	case BSSMAP_RESET_EV_CONN_CFM_SUCCESS:
		bssmap_reset->conn_cfm_failures = 0;
		break;

	default:
		OSMO_ASSERT(false);
	}
}

static int bssmap_reset_fsm_timer_cb(struct osmo_fsm_inst *fi)
{
	struct bssmap_reset *bssmap_reset = (struct bssmap_reset*)fi->priv;

	tx_reset(bssmap_reset);

	/* (re-)enter disconnect state to resend RESET after timeout. */
	bssmap_reset_fsm_state_chg(fi, BSSMAP_RESET_ST_DISC);

	/* Return 0 to not terminate the fsm */
	return 0;
}

#define S(x) (1 << (x))

static struct osmo_fsm_state bssmap_reset_fsm_states[] = {
	[BSSMAP_RESET_ST_DISC] = {
		     .name = "DISC",
		     .in_event_mask = 0
			     | S(BSSMAP_RESET_EV_RX_RESET_ACK)
			     ,
		     .out_state_mask = 0
			     | S(BSSMAP_RESET_ST_DISC)
			     | S(BSSMAP_RESET_ST_CONN)
			     ,
		     .onenter = bssmap_reset_disc_onenter,
		     .action = bssmap_reset_disc_action,
		     },
	[BSSMAP_RESET_ST_CONN] = {
		     .name = "CONN",
		     .in_event_mask = 0
			     | S(BSSMAP_RESET_EV_RX_RESET_ACK)
			     | S(BSSMAP_RESET_EV_CONN_CFM_FAILURE)
			     | S(BSSMAP_RESET_EV_CONN_CFM_SUCCESS)
			     ,
		     .out_state_mask = 0
			     | S(BSSMAP_RESET_ST_DISC)
			     | S(BSSMAP_RESET_ST_CONN)
			     ,
		     .onenter = bssmap_reset_conn_onenter,
		     .action = bssmap_reset_conn_action,
		     },
};

static struct osmo_fsm bssmap_reset_fsm = {
	.name = "bssmap_reset",
	.states = bssmap_reset_fsm_states,
	.num_states = ARRAY_SIZE(bssmap_reset_fsm_states),
	.log_subsys = DRESET,
	.timer_cb = bssmap_reset_fsm_timer_cb,
	.event_names = bssmap_reset_fsm_event_names,
};

bool bssmap_reset_is_conn_ready(const struct bssmap_reset *bssmap_reset)
{
	return bssmap_reset->fi->state == BSSMAP_RESET_ST_CONN;
}

static __attribute__((constructor)) void bssmap_reset_fsm_init()
{
	OSMO_ASSERT(osmo_fsm_register(&bssmap_reset_fsm) == 0);
}