OpenVPN
schedule.h
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2026 OpenVPN Inc <sales@openvpn.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifndef SCHEDULE_H
24#define SCHEDULE_H
25
26/*
27 * This code implements an efficient scheduler using
28 * a random treap binary tree.
29 *
30 * The scheduler is used by the server executive to
31 * keep track of which instances need service at a
32 * known time in the future. Instances need to
33 * schedule events for things such as sending
34 * a ping or scheduling a TLS renegotiation.
35 */
36
37#include "otime.h"
38#include "error.h"
39
41{
42 struct timeval tv; /* wakeup time */
43 unsigned int pri; /* random treap priority */
44 struct schedule_entry *parent; /* treap (btree) links */
47};
48
50{
51 struct schedule_entry *earliest_wakeup; /* cached earliest wakeup */
52 struct schedule_entry *root; /* the root of the treap (btree) */
53};
54
55/* Public functions */
56
57struct schedule *schedule_init(void);
58
59void schedule_free(struct schedule *s);
60
61void schedule_remove_entry(struct schedule *s, struct schedule_entry *e);
62
63/* Private Functions */
64
65/* is node already in tree? */
66#define IN_TREE(e) ((e)->pri)
67
69
70void schedule_add_modify(struct schedule *s, struct schedule_entry *e);
71
72void schedule_remove_node(struct schedule *s, struct schedule_entry *e);
73
74/* Public inline functions */
75
97static inline void
98schedule_add_entry(struct schedule *s, struct schedule_entry *e, const struct timeval *tv,
99 unsigned int sigma)
100{
101 if (!IN_TREE(e) || !sigma || !tv_within_sigma(tv, &e->tv, sigma))
102 {
103 e->tv = *tv;
105 s->earliest_wakeup = NULL; /* invalidate cache */
106 }
107}
108
109/*
110 * Return the node with the earliest wakeup time. If two
111 * nodes have the exact same wakeup time, select based on
112 * the random priority assigned to each node (the priority
113 * is randomized every time an entry is re-added).
114 */
115static inline struct schedule_entry *
116schedule_get_earliest_wakeup(struct schedule *s, struct timeval *wakeup)
117{
118 struct schedule_entry *ret;
119
120 /* cache result */
121 if (!s->earliest_wakeup)
122 {
124 }
125 ret = s->earliest_wakeup;
126 if (ret)
127 {
128 *wakeup = ret->tv;
129 }
130
131 return ret;
132}
133
142int
143schedule_entry_compare(const struct schedule_entry *e1, const struct schedule_entry *e2);
144#endif /* ifndef SCHEDULE_H */
static bool tv_within_sigma(const struct timeval *t1, const struct timeval *t2, unsigned int sigma)
Definition otime.h:181
void schedule_remove_entry(struct schedule *s, struct schedule_entry *e)
Definition schedule.c:388
struct schedule * schedule_init(void)
Definition schedule.c:373
void schedule_remove_node(struct schedule *s, struct schedule_entry *e)
Definition schedule.c:220
static struct schedule_entry * schedule_get_earliest_wakeup(struct schedule *s, struct timeval *wakeup)
Definition schedule.h:116
struct schedule_entry * schedule_find_least(struct schedule_entry *e)
Definition schedule.c:348
int schedule_entry_compare(const struct schedule_entry *e1, const struct schedule_entry *e2)
This method compares two schedule entries and return which one is earlier,later or equal.
Definition schedule.c:65
static void schedule_add_entry(struct schedule *s, struct schedule_entry *e, const struct timeval *tv, unsigned int sigma)
Add a struct schedule_entry to the scheduler btree or update an existing entry with a new wakeup time...
Definition schedule.h:98
void schedule_add_modify(struct schedule *s, struct schedule_entry *e)
Definition schedule.c:309
void schedule_free(struct schedule *s)
Definition schedule.c:382
#define IN_TREE(e)
Definition schedule.h:66
Definition schedule.h:41
unsigned int pri
Definition schedule.h:43
struct timeval tv
Definition schedule.h:42
struct schedule_entry * lt
Definition schedule.h:45
struct schedule_entry * parent
Definition schedule.h:44
struct schedule_entry * gt
Definition schedule.h:46
struct schedule_entry * earliest_wakeup
Definition schedule.h:51
struct schedule_entry * root
Definition schedule.h:52