aboutsummaryrefslogtreecommitdiffstats
path: root/src/osmo-bsc/penalty_timers.c
blob: 02cf2468a0981c0e506c35293cbf84f3e1f18d74 (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
/* (C) 2018 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
 *
 * All Rights Reserved
 *
 * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de>
 *
 * 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 <talloc.h>
#include <time.h>
#include <stdint.h>

#include <osmocom/core/linuxlist.h>

#include <osmocom/bsc/penalty_timers.h>
#include <osmocom/bsc/gsm_data.h>

struct penalty_timers {
	struct llist_head timers;
};

struct penalty_timer {
	struct llist_head entry;
	const void *for_object;
	unsigned int timeout;
};

static unsigned int time_now(void)
{
	time_t now;
	time(&now);
	/* FIXME: use monotonic clock */
	return (unsigned int)now;
}

struct penalty_timers *penalty_timers_init(void *ctx)
{
	struct penalty_timers *pt = talloc_zero(ctx, struct penalty_timers);
	if (!pt)
		return NULL;
	INIT_LLIST_HEAD(&pt->timers);
	return pt;
}

void penalty_timers_add(struct penalty_timers *pt, const void *for_object, int timeout)
{
	struct penalty_timer *timer;
	unsigned int now;
	unsigned int then;
	now = time_now();

	if (timeout <= 0)
		return;

	then = now + timeout;

	/* timer already running for that BTS? */
	llist_for_each_entry(timer, &pt->timers, entry) {
		if (timer->for_object != for_object)
			continue;
		/* raise, if running timer will timeout earlier or has timed
		 * out already, otherwise keep later timeout */
		if (timer->timeout < then)
			timer->timeout = then;
		return;
	}

	/* add new timer */
	timer = talloc_zero(pt, struct penalty_timer);
	if (!timer)
		return;

	timer->for_object = for_object;
	timer->timeout = then;

	llist_add_tail(&timer->entry, &pt->timers);
}

unsigned int penalty_timers_remaining(struct penalty_timers *pt, const void *for_object)
{
	struct penalty_timer *timer;
	unsigned int now = time_now();
	unsigned int max_remaining = 0;
	llist_for_each_entry(timer, &pt->timers, entry) {
		unsigned int remaining;
		if (timer->for_object != for_object)
			continue;
		if (now >= timer->timeout)
			continue;
		remaining = timer->timeout - now;
		if (remaining > max_remaining)
			max_remaining = remaining;
	}
	return max_remaining;
}

void penalty_timers_clear(struct penalty_timers *pt, const void *for_object)
{
	struct penalty_timer *timer, *timer2;
	llist_for_each_entry_safe(timer, timer2, &pt->timers, entry) {
		if (for_object && timer->for_object != for_object)
			continue;
		llist_del(&timer->entry);
		talloc_free(timer);
	}
}

void penalty_timers_free(struct penalty_timers **pt_p)
{
	struct penalty_timers *pt = *pt_p;
	if (!pt)
		return;
	penalty_timers_clear(pt, NULL);
	talloc_free(pt);
	*pt_p = NULL;
}