OpenVPN
shaper.h
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single 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-2025 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 SHAPER_H
24#define SHAPER_H
25
26/*#define SHAPER_DEBUG*/
27
28#include "basic.h"
29#include "integer.h"
30#include "misc.h"
31#include "error.h"
32#include "interval.h"
33
34/*
35 * A simple traffic shaper for
36 * the output direction.
37 */
38
39#define SHAPER_MIN 100 /* bytes per second */
40#define SHAPER_MAX 100000000
41
42#define SHAPER_MAX_TIMEOUT 10 /* seconds */
43
44#define SHAPER_USE_FP
45
46struct shaper
47{
49 struct timeval wakeup;
50
51#ifdef SHAPER_USE_FP
52 double factor;
53#else
54 int factor;
55#endif
56};
57
58void shaper_msg(struct shaper *s);
59
60void shaper_reset_wakeup(struct shaper *s);
61
62/*
63 * We want to wake up in delay microseconds. If timeval is larger
64 * than delay, set timeval to delay.
65 */
66bool shaper_soonest_event(struct timeval *tv, int delay);
67
68/*
69 * inline functions
70 */
71
72static inline void
73shaper_reset(struct shaper *s, int bytes_per_second)
74{
75 s->bytes_per_second = constrain_int(bytes_per_second, SHAPER_MIN, SHAPER_MAX);
76
77#ifdef SHAPER_USE_FP
78 s->factor = 1000000.0 / (double)s->bytes_per_second;
79#else
80 s->factor = 1000000 / s->bytes_per_second;
81#endif
82}
83
84static inline void
85shaper_init(struct shaper *s, int bytes_per_second)
86{
87 shaper_reset(s, bytes_per_second);
89}
90
91/*
92 * Returns traffic shaping delay in microseconds relative to current
93 * time, or 0 if no delay.
94 */
95static inline int
97{
98 struct timeval tv;
99 int delay = 0;
100
101 if (tv_defined(&s->wakeup))
102 {
103 ASSERT(!openvpn_gettimeofday(&tv, NULL));
104 delay = tv_subtract(&s->wakeup, &tv, SHAPER_MAX_TIMEOUT);
105#ifdef SHAPER_DEBUG
106 dmsg(D_SHAPER_DEBUG, "SHAPER shaper_delay delay=%d", delay);
107#endif
108 }
109
110 return delay > 0 ? delay : 0;
111}
112
113
114/*
115 * We are about to send a datagram of nbytes bytes.
116 *
117 * Compute when we can send another datagram,
118 * based on target throughput (s->bytes_per_second).
119 */
120static inline void
121shaper_wrote_bytes(struct shaper *s, int nbytes)
122{
123 struct timeval tv;
124
125 /* compute delay in microseconds */
126 tv.tv_sec = 0;
127#ifdef SHAPER_USE_FP
128 tv.tv_usec =
129 min_int((int)((double)max_int(nbytes, 100) * s->factor), (SHAPER_MAX_TIMEOUT * 1000000));
130#else
131 tv.tv_usec = s->bytes_per_second
132 ? min_int(max_int(nbytes, 100) * s->factor, (SHAPER_MAX_TIMEOUT * 1000000))
133 : 0;
134#endif
135
136 if (tv.tv_usec)
137 {
139 tv_add(&s->wakeup, &tv);
140
141#ifdef SHAPER_DEBUG
143 "SHAPER shaper_wrote_bytes bytes=%d delay=%ld sec=%" PRIi64 " usec=%ld", nbytes,
144 (long)tv.tv_usec, (int64_t)s->wakeup.tv_sec, (long)s->wakeup.tv_usec);
145#endif
146 }
147}
148
149#if 0
150/*
151 * Increase/Decrease bandwidth by a percentage.
152 *
153 * Return true if bandwidth changed.
154 */
155static inline bool
156shaper_change_pct(struct shaper *s, int pct)
157{
158 const int orig_bandwidth = s->bytes_per_second;
159 const int new_bandwidth = orig_bandwidth + (orig_bandwidth * pct / 100);
161 shaper_reset(s, new_bandwidth);
162 return s->bytes_per_second != orig_bandwidth;
163}
164#endif
165
166#endif /* ifndef SHAPER_H */
#define D_SHAPER_DEBUG
Definition errlevel.h:175
static int min_int(int x, int y)
Definition integer.h:105
static int max_int(int x, int y)
Definition integer.h:92
static int constrain_int(int x, int min, int max)
Definition integer.h:118
#define dmsg(flags,...)
Definition error.h:170
#define ASSERT(x)
Definition error.h:217
static int tv_subtract(const struct timeval *tv1, const struct timeval *tv2, const unsigned int max_seconds)
Definition otime.h:114
static int openvpn_gettimeofday(struct timeval *tv, void *tz)
Definition otime.h:63
static bool tv_defined(const struct timeval *tv)
Definition otime.h:107
static void tv_add(struct timeval *dest, const struct timeval *src)
Definition otime.h:131
void shaper_msg(struct shaper *s)
Definition shaper.c:87
static void shaper_wrote_bytes(struct shaper *s, int nbytes)
Definition shaper.h:121
static void shaper_reset(struct shaper *s, int bytes_per_second)
Definition shaper.h:73
#define SHAPER_MAX_TIMEOUT
Definition shaper.h:42
void shaper_reset_wakeup(struct shaper *s)
Definition shaper.c:81
static void shaper_init(struct shaper *s, int bytes_per_second)
Definition shaper.h:85
#define SHAPER_MAX
Definition shaper.h:40
static int shaper_delay(struct shaper *s)
Definition shaper.h:96
#define SHAPER_MIN
Definition shaper.h:39
bool shaper_soonest_event(struct timeval *tv, int delay)
Definition shaper.c:36
double factor
Definition shaper.h:52
struct timeval wakeup
Definition shaper.h:49
int bytes_per_second
Definition shaper.h:48