aboutsummaryrefslogtreecommitdiffstats
path: root/src/neigh_cache.c
blob: 129e40b99a9d8ea1011e1902dced42c21a3a1c3b (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* si_cache.c
 *
 * Copyright (C) 2021 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
 * Author: Pau Espin Pedrol <pespin@sysmocom.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * 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 General Public License for more details.
 */

#include <string.h>
#include <talloc.h>
#include <inttypes.h>

#include <osmocom/core/utils.h>

#include <neigh_cache.h>
#include <gprs_debug.h>

static void neigh_cache_schedule_cleanup(struct neigh_cache *cache);
static void neigh_cache_cleanup_cb(void *data)
{
	struct timespec now, threshold;
	struct neigh_cache *cache = (struct neigh_cache *)data;
	struct neigh_cache_entry *it, *tmp;

	osmo_clock_gettime(CLOCK_MONOTONIC, &now);

	/* Instead of adding keep_time_intval to each, substract it from now once */
	timespecsub(&now, &cache->keep_time_intval, &threshold);

	llist_for_each_entry_safe(it, tmp, &cache->list, list) {
		if (timespeccmp(&threshold, &it->update_ts, <))
			break;
		LOGP(DNACC, LOGL_DEBUG,
		     "neigh_cache: Removing entry " NEIGH_CACHE_ENTRY_KEY_FMT " => %s\n",
		     NEIGH_CACHE_ENTRY_KEY_ARGS(&it->key), osmo_cgi_ps_name(&it->value));
		llist_del(&it->list);
		talloc_free(it);
	}

	neigh_cache_schedule_cleanup(cache);
}

static void neigh_cache_schedule_cleanup(struct neigh_cache *cache)
{
	struct neigh_cache_entry *it;
	struct timespec now, threshold, result;

	/* First item is the one with oldest update_ts */
	it = llist_first_entry_or_null(&cache->list, struct neigh_cache_entry, list);
	if (!it)
		return;

	osmo_clock_gettime(CLOCK_MONOTONIC, &now);

	timespecadd(&it->update_ts, &cache->keep_time_intval, &threshold);

	if (timespeccmp(&now, &threshold, >=)) {
		/* Too late, let's flush asynchonously so newly added isn't
		 * immediatelly freed before return. */
		result = (struct timespec){ .tv_sec = 0, .tv_nsec = 0 };
	} else {
		timespecsub(&threshold, &now, &result);
	}
	osmo_timer_schedule(&cache->cleanup_timer, result.tv_sec, result.tv_nsec / 1000);
}

struct neigh_cache *neigh_cache_alloc(void *ctx, unsigned int keep_time_sec)
{
	struct neigh_cache *cache = talloc_zero(ctx, struct neigh_cache);
	OSMO_ASSERT(cache);
	INIT_LLIST_HEAD(&cache->list);
	osmo_timer_setup(&cache->cleanup_timer, neigh_cache_cleanup_cb, cache);
	cache->keep_time_intval = (struct timespec){ .tv_sec = keep_time_sec, .tv_nsec = 0};
	return cache;

}

void neigh_cache_set_keep_time_interval(struct neigh_cache *cache, unsigned int keep_time_sec)
{
	cache->keep_time_intval = (struct timespec){ .tv_sec = keep_time_sec, .tv_nsec = 0};
	neigh_cache_schedule_cleanup(cache);
}

static struct neigh_cache_entry *neigh_cache_lookup_entry(struct neigh_cache *cache,
							  const struct neigh_cache_entry_key *key)
{
	struct neigh_cache_entry *tmp;
	llist_for_each_entry(tmp, &cache->list, list) {
		if (neigh_cache_entry_key_eq(&tmp->key, key))
			return tmp;
	}
	return NULL;
}

const struct osmo_cell_global_id_ps *neigh_cache_lookup_value(struct neigh_cache *cache,
							      const struct neigh_cache_entry_key *key)
{
	struct neigh_cache_entry *it = neigh_cache_lookup_entry(cache, key);
	if (it)
		return &it->value;
	return NULL;
}

struct neigh_cache_entry *neigh_cache_add(struct neigh_cache *cache,
					  const struct neigh_cache_entry_key *key,
					  const struct osmo_cell_global_id_ps *value)
{
	struct neigh_cache_entry *it;

	/* First check if it already exists. If so, simply update timer+value */
	it = neigh_cache_lookup_entry(cache, key);
	if (!it) {
		LOGP(DNACC, LOGL_DEBUG,
		     "neigh_cache: Inserting new entry " NEIGH_CACHE_ENTRY_KEY_FMT " => %s\n",
		     NEIGH_CACHE_ENTRY_KEY_ARGS(key), osmo_cgi_ps_name(value));
		it = talloc_zero(cache, struct neigh_cache_entry);
		OSMO_ASSERT(it);
		memcpy(&it->key, key, sizeof(it->key));
	} else {
		LOGP(DNACC, LOGL_DEBUG,
		     "neigh_cache: Updating entry " NEIGH_CACHE_ENTRY_KEY_FMT " => (%s -> %s)\n",
		     NEIGH_CACHE_ENTRY_KEY_ARGS(key), osmo_cgi_ps_name(&it->value), osmo_cgi_ps_name2(value));
		/* remove item, we'll add it to the end to have them sorted by last update */
		llist_del(&it->list);
	}

	memcpy(&it->value, value, sizeof(it->value));
	OSMO_ASSERT(osmo_clock_gettime(CLOCK_MONOTONIC, &it->update_ts) == 0);
	llist_add_tail(&it->list, &cache->list);
	neigh_cache_schedule_cleanup(cache);
	return it;
}

void neigh_cache_free(struct neigh_cache *cache)
{
	struct neigh_cache_entry *it, *tmp;
	if (!cache)
		return;

	llist_for_each_entry_safe(it, tmp, &cache->list, list) {
		llist_del(&it->list);
		talloc_free(it);
	}
	osmo_timer_del(&cache->cleanup_timer);
	talloc_free(cache);
}


////////////////////
// SI CACHE
///////////////////

static void si_cache_schedule_cleanup(struct si_cache *cache);
static void si_cache_cleanup_cb(void *data)
{
	struct timespec now, threshold;
	struct si_cache *cache = (struct si_cache *)data;
	struct si_cache_entry *it, *tmp;

	osmo_clock_gettime(CLOCK_MONOTONIC, &now);

	/* Instead of adding keep_time_intval to each, substract it from now once */
	timespecsub(&now, &cache->keep_time_intval, &threshold);

	llist_for_each_entry_safe(it, tmp, &cache->list, list) {
		if (timespeccmp(&threshold, &it->update_ts, <))
			break;
		LOGP(DNACC, LOGL_DEBUG, "si_cache: Removing entry %s\n",
		     osmo_cgi_ps_name(&it->key));
		llist_del(&it->list);
		talloc_free(it);
	}

	si_cache_schedule_cleanup(cache);
}

static void si_cache_schedule_cleanup(struct si_cache *cache)
{
	struct si_cache_entry *it;
	struct timespec now, threshold, result;

	/* First item is the one with oldest update_ts */
	it = llist_first_entry_or_null(&cache->list, struct si_cache_entry, list);
	if (!it)
		return;

	osmo_clock_gettime(CLOCK_MONOTONIC, &now);

	timespecadd(&it->update_ts, &cache->keep_time_intval, &threshold);

	if (timespeccmp(&now, &threshold, >=)) {
		/* Too late, let's flush asynchonously so newly added isn't
		 * immediatelly freed before return. */
		result = (struct timespec){ .tv_sec = 0, .tv_nsec = 0 };
	} else {
		timespecsub(&threshold, &now, &result);
	}
	osmo_timer_schedule(&cache->cleanup_timer, result.tv_sec, result.tv_nsec / 1000);
}

struct si_cache *si_cache_alloc(void *ctx, unsigned int keep_time_sec)
{
	struct si_cache *cache = talloc_zero(ctx, struct si_cache);
	OSMO_ASSERT(cache);
	INIT_LLIST_HEAD(&cache->list);
	osmo_timer_setup(&cache->cleanup_timer, si_cache_cleanup_cb, cache);
	cache->keep_time_intval = (struct timespec){ .tv_sec = keep_time_sec, .tv_nsec = 0};
	return cache;
}

void si_cache_set_keep_time_interval(struct si_cache *cache, unsigned int keep_time_sec)
{
	cache->keep_time_intval = (struct timespec){ .tv_sec = keep_time_sec, .tv_nsec = 0};
	si_cache_schedule_cleanup(cache);
}

struct si_cache_entry *si_cache_add(struct si_cache *cache,
				    const struct osmo_cell_global_id_ps *key,
				    const struct si_cache_value *value)
{
	struct si_cache_entry *it;

	/* First check if it already exists. If so, simply update timer+value */
	it = si_cache_lookup_entry(cache, key);
	if (!it) {
		LOGP(DNACC, LOGL_DEBUG, "si_cache: Inserting new entry %s\n",
		     osmo_cgi_ps_name(key));
		it = talloc_zero(cache, struct si_cache_entry);
		OSMO_ASSERT(it);
		memcpy(&it->key, key, sizeof(it->key));
	} else {
		LOGP(DNACC, LOGL_DEBUG, "si_cache: Updating entry %s\n",
		     osmo_cgi_ps_name(&it->key));
		/* remove item, we'll add it to the end to have them sorted by last update */
		llist_del(&it->list);
	}

	memcpy(&it->value, value, sizeof(it->value));
	OSMO_ASSERT(osmo_clock_gettime(CLOCK_MONOTONIC, &it->update_ts) == 0);
	llist_add_tail(&it->list, &cache->list);
	si_cache_schedule_cleanup(cache);
	return it;
}

struct si_cache_entry *si_cache_lookup_entry(struct si_cache *cache,
					     const struct osmo_cell_global_id_ps *key)
{
	struct si_cache_entry *tmp;
	llist_for_each_entry(tmp, &cache->list, list) {
		if (osmo_cgi_ps_cmp(&tmp->key, key) == 0)
			return tmp;
	}
	return NULL;
}

const struct si_cache_value *si_cache_lookup_value(struct si_cache *cache,
						   const struct osmo_cell_global_id_ps *key)
{
	struct si_cache_entry *it = si_cache_lookup_entry(cache, key);
	if (it)
		return &it->value;
	return NULL;
}

void si_cache_free(struct si_cache *cache)
{
	struct si_cache_entry *it, *tmp;
	if (!cache)
		return;

	llist_for_each_entry_safe(it, tmp, &cache->list, list) {
		llist_del(&it->list);
		talloc_free(it);
	}
	osmo_timer_del(&cache->cleanup_timer);
	talloc_free(cache);
}