summaryrefslogtreecommitdiffstats
path: root/pceplib/pcep_timers.c
blob: 79ae563f140b5b4e58cafde7c17e3fac47c41364 (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
 * This file is part of the PCEPlib, a PCEP protocol library.
 *
 * Copyright (C) 2020 Volta Networks https://voltanet.io/
 *
 * Author : Brady Johnson <brady@voltanet.io>
 *
 */


/*
 *  Implementation of public API timer functions.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <limits.h>
#include <pthread.h>
#include <stddef.h>
#include <stdbool.h>
#include <string.h>

#include "pcep_timers.h"
#include "pcep_utils_logging.h"
#include "pcep_utils_memory.h"
#include "pcep_utils_ordered_list.h"

static pcep_timers_context *timers_context_ = NULL;
static int timer_id_ = 0;


/* simple compare method callback used by pcep_utils_ordered_list
 * for ordered list insertion. */
int timer_list_node_compare(void *list_entry, void *new_entry)
{
	/* return:
	 *   < 0  if new_entry < list_entry
	 *   == 0 if new_entry == list_entry (new_entry will be inserted after
	 * list_entry) > 0  if new_entry > list_entry */
	return ((pcep_timer *)new_entry)->expire_time
	       - ((pcep_timer *)list_entry)->expire_time;
}


/* simple compare method callback used by pcep_utils_ordered_list
 * ordered_list_remove_first_node_equals2 to remove a timer based on
 * its timer_id. */
int timer_list_node_timer_id_compare(void *list_entry, void *new_entry)
{
	return ((pcep_timer *)new_entry)->timer_id
	       - ((pcep_timer *)list_entry)->timer_id;
}

/* simple compare method callback used by pcep_utils_ordered_list
 * ordered_list_remove_first_node_equals2 to remove a timer based on
 * its address. */
int timer_list_node_timer_ptr_compare(void *list_entry, void *new_entry)
{
	return ((char *)new_entry - (char *)list_entry);
}

/* internal util method */
static pcep_timers_context *create_timers_context_(void)
{
	if (timers_context_ == NULL) {
		timers_context_ = pceplib_malloc(PCEPLIB_INFRA,
						 sizeof(pcep_timers_context));
		memset(timers_context_, 0, sizeof(pcep_timers_context));
		timers_context_->active = false;
	}

	return timers_context_;
}


/* Internal util function */
static bool initialize_timers_common(timer_expire_handler expire_handler)
{
	if (expire_handler == NULL) {
		/* Cannot have a NULL handler function */
		return false;
	}

	timers_context_ = create_timers_context_();

	if (timers_context_->active == true) {
		/* already initialized */
		return false;
	}

	timers_context_->active = true;
	timers_context_->timer_list =
		ordered_list_initialize(timer_list_node_compare);
	timers_context_->expire_handler = expire_handler;

	if (pthread_mutex_init(&(timers_context_->timer_list_lock), NULL)
	    != 0) {
		pcep_log(
			LOG_ERR,
			"%s: ERROR initializing timers, cannot initialize the mutex",
			__func__);
		return false;
	}

	return true;
}

bool initialize_timers(timer_expire_handler expire_handler)
{
	if (initialize_timers_common(expire_handler) == false) {
		return false;
	}

	if (pthread_create(&(timers_context_->event_loop_thread), NULL,
			   event_loop, timers_context_)) {
		pcep_log(
			LOG_ERR,
			"%s: ERROR initializing timers, cannot initialize the thread",
			__func__);
		return false;
	}

	return true;
}

bool initialize_timers_external_infra(
	timer_expire_handler expire_handler, void *external_timer_infra_data,
	ext_timer_create timer_create_func, ext_timer_cancel timer_cancel_func,
	ext_pthread_create_callback thread_create_func)
{
	if (initialize_timers_common(expire_handler) == false) {
		return false;
	}

	if (thread_create_func != NULL) {
		if (thread_create_func(&(timers_context_->event_loop_thread),
				       NULL, event_loop, timers_context_,
				       "pceplib_timers")) {
			pcep_log(
				LOG_ERR,
				"%s: Cannot initialize external timers thread.",
				__func__);
			return false;
		}
	} else {
		if (pthread_create(&(timers_context_->event_loop_thread), NULL,
				   event_loop, timers_context_)) {
			pcep_log(
				LOG_ERR,
				"%s: ERROR initializing timers, cannot initialize the thread",
				__func__);
			return false;
		}
	}

	timers_context_->external_timer_infra_data = external_timer_infra_data;
	timers_context_->timer_create_func = timer_create_func;
	timers_context_->timer_cancel_func = timer_cancel_func;

	return true;
}

/*
 * This function is only used to tear_down the timer data.
 * Only the timer data is deleted, not the list itself,
 * which is deleted by ordered_list_destroy().
 */
void free_all_timers(pcep_timers_context *timers_context)
{
	pthread_mutex_lock(&timers_context->timer_list_lock);

	ordered_list_node *timer_node = timers_context->timer_list->head;

	while (timer_node != NULL) {
		if (timer_node->data != NULL) {
			pceplib_free(PCEPLIB_INFRA, timer_node->data);
		}
		timer_node = timer_node->next_node;
	}

	pthread_mutex_unlock(&timers_context->timer_list_lock);
}


bool teardown_timers(void)
{
	if (timers_context_ == NULL) {
		pcep_log(
			LOG_WARNING,
			"%s: Trying to teardown the timers, but they are not initialized",
			__func__);
		return false;
	}

	if (timers_context_->active == false) {
		pcep_log(
			LOG_WARNING,
			"%s: Trying to teardown the timers, but they are not active",
			__func__);
		return false;
	}

	timers_context_->active = false;
	if (timers_context_->event_loop_thread != 0) {
		/* TODO this does not build
		 * Instead of calling pthread_join() which could block if the
		thread
		 * is blocked, try joining for at most 1 second.
		struct timespec ts;
		clock_gettime(CLOCK_REALTIME, &ts);
		ts.tv_sec += 1;
		int retval =
		pthread_timedjoin_np(timers_context_->event_loop_thread, NULL,
		&ts); if (retval != 0)
		{
		    pcep_log(LOG_WARNING, "%s: thread did not stop after 1
		second waiting on it.", __func__);
		}
		*/
		pthread_join(timers_context_->event_loop_thread, NULL);
	}

	free_all_timers(timers_context_);
	ordered_list_destroy(timers_context_->timer_list);

	if (pthread_mutex_destroy(&(timers_context_->timer_list_lock)) != 0) {
		pcep_log(
			LOG_WARNING,
			"%s: Trying to teardown the timers, cannot destroy the mutex",
			__func__);
	}

	pceplib_free(PCEPLIB_INFRA, timers_context_);
	timers_context_ = NULL;

	return true;
}


int get_next_timer_id(void)
{
	if (timer_id_ == INT_MAX) {
		timer_id_ = 0;
	}

	return timer_id_++;
}

int create_timer(uint16_t sleep_seconds, void *data)
{
	if (timers_context_ == NULL) {
		pcep_log(
			LOG_WARNING,
			"%s: Trying to create a timer: the timers have not been initialized",
			__func__);
		return -1;
	}

	pcep_timer *timer = pceplib_malloc(PCEPLIB_INFRA, sizeof(pcep_timer));
	memset(timer, 0, sizeof(pcep_timer));
	timer->data = data;
	timer->sleep_seconds = sleep_seconds;
	timer->expire_time = time(NULL) + sleep_seconds;

	pthread_mutex_lock(&timers_context_->timer_list_lock);
	timer->timer_id = get_next_timer_id();

	/* implemented in pcep_utils_ordered_list.c */
	if (ordered_list_add_node(timers_context_->timer_list, timer) == NULL) {
		pceplib_free(PCEPLIB_INFRA, timer);
		pthread_mutex_unlock(&timers_context_->timer_list_lock);
		pcep_log(
			LOG_WARNING,
			"%s: Trying to create a timer, cannot add the timer to the timer list",
			__func__);

		return -1;
	}

	pthread_mutex_unlock(&timers_context_->timer_list_lock);

	if (timers_context_->timer_create_func) {
		timers_context_->timer_create_func(
			timers_context_->external_timer_infra_data,
			&timer->external_timer, sleep_seconds, timer);
	}

	return timer->timer_id;
}


bool cancel_timer(int timer_id)
{
	static pcep_timer compare_timer;

	if (timers_context_ == NULL) {
		pcep_log(
			LOG_WARNING,
			"%s: Trying to cancel a timer: the timers have not been initialized",
			__func__);
		return false;
	}

	pthread_mutex_lock(&timers_context_->timer_list_lock);

	compare_timer.timer_id = timer_id;
	pcep_timer *timer_toRemove = ordered_list_remove_first_node_equals2(
		timers_context_->timer_list, &compare_timer,
		timer_list_node_timer_id_compare);
	if (timer_toRemove == NULL) {
		pthread_mutex_unlock(&timers_context_->timer_list_lock);
		pcep_log(
			LOG_WARNING,
			"%s: Trying to cancel a timer [%d] that does not exist",
			__func__, timer_id);
		return false;
	}

	pthread_mutex_unlock(&timers_context_->timer_list_lock);

	if (timers_context_->timer_cancel_func) {
		timers_context_->timer_cancel_func(
			&timer_toRemove->external_timer);
	}

	pceplib_free(PCEPLIB_INFRA, timer_toRemove);

	return true;
}


bool reset_timer(int timer_id)
{
	static pcep_timer compare_timer;

	if (timers_context_ == NULL) {
		pcep_log(
			LOG_WARNING,
			"%s: Trying to reset a timer: the timers have not been initialized",
			__func__);

		return false;
	}

	pthread_mutex_lock(&timers_context_->timer_list_lock);

	compare_timer.timer_id = timer_id;
	ordered_list_node *timer_to_reset_node =
		ordered_list_find2(timers_context_->timer_list, &compare_timer,
				   timer_list_node_timer_id_compare);
	if (timer_to_reset_node == NULL) {
		pthread_mutex_unlock(&timers_context_->timer_list_lock);
		pcep_log(LOG_WARNING,
			 "%s: Trying to reset a timer node that does not exist",
			 __func__);

		return false;
	}

	pcep_timer *timer_to_reset = timer_to_reset_node->data;
	if (timer_to_reset == NULL) {
		pthread_mutex_unlock(&timers_context_->timer_list_lock);
		pcep_log(LOG_WARNING,
			 "%s: Trying to reset a timer that does not exist",
			 __func__);

		return false;
	}

	/* First check if the timer to reset already has the same expire time,
	 * which means multiple reset_timer() calls were made on the same timer
	 * in the same second */
	time_t expire_time = time(NULL) + timer_to_reset->sleep_seconds;
	if (timer_to_reset->expire_time == expire_time) {
		pthread_mutex_unlock(&timers_context_->timer_list_lock);
		return true;
	}

	ordered_list_remove_node2(timers_context_->timer_list,
				  timer_to_reset_node);

	timer_to_reset->expire_time = expire_time;
	if (ordered_list_add_node(timers_context_->timer_list, timer_to_reset)
	    == NULL) {
		pceplib_free(PCEPLIB_INFRA, timer_to_reset);
		pthread_mutex_unlock(&timers_context_->timer_list_lock);
		pcep_log(
			LOG_WARNING,
			"%s: Trying to reset a timer, cannot add the timer to the timer list",
			__func__);

		return false;
	}

	pthread_mutex_unlock(&timers_context_->timer_list_lock);

	if (timers_context_->timer_cancel_func) {
		/* Keeping this log for now, since in older versions of FRR the
		 * timer cancellation was blocking. This allows us to see how
		 * long the it takes.*/
		pcep_log(LOG_DEBUG, "%s: Resetting timer [%d] with callback",
			 __func__, timer_to_reset->timer_id);
		timers_context_->timer_cancel_func(
			&timer_to_reset->external_timer);
		timer_to_reset->external_timer = NULL;
	}

	if (timers_context_->timer_create_func) {
		timers_context_->timer_create_func(
			timers_context_->external_timer_infra_data,
			&timer_to_reset->external_timer,
			timer_to_reset->sleep_seconds, timer_to_reset);
		/* Keeping this log for now, since in older versions of FRR the
		 * timer cancellation was blocking. This allows us to see how
		 * long the it takes.*/
		pcep_log(LOG_DEBUG, "%s: Reset timer [%d] with callback",
			 __func__, timer_to_reset->timer_id);
	}

	return true;
}


void pceplib_external_timer_expire_handler(void *data)
{
	if (timers_context_ == NULL) {
		pcep_log(
			LOG_WARNING,
			"%s: External timer expired but timers_context is not initialized",
			__func__);
		return;
	}

	if (timers_context_->expire_handler == NULL) {
		pcep_log(
			LOG_WARNING,
			"%s: External timer expired but expire_handler is not initialized",
			__func__);
		return;
	}

	if (data == NULL) {
		pcep_log(LOG_WARNING,
			 "%s: External timer expired with NULL data", __func__);
		return;
	}

	pcep_timer *timer = (pcep_timer *)data;

	pthread_mutex_lock(&timers_context_->timer_list_lock);
	ordered_list_node *timer_node =
		ordered_list_find2(timers_context_->timer_list, timer,
				   timer_list_node_timer_ptr_compare);

	/* Remove timer from list */
	if (timer_node)
		ordered_list_remove_node2(timers_context_->timer_list,
					  timer_node);

	pthread_mutex_unlock(&timers_context_->timer_list_lock);

	/* Cannot continue if the timer does not exist */
	if (timer_node == NULL) {
		pcep_log(
			LOG_WARNING,
			"%s: pceplib_external_timer_expire_handler timer [%p] id [%d] does not exist",
			__func__, timer, timer->timer_id);
		return;
	}

	timers_context_->expire_handler(timer->data, timer->timer_id);

	pceplib_free(PCEPLIB_INFRA, timer);
}