OpenVPN
shaper.c
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#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include "syshead.h"
28#include "shaper.h"
29#include "memdbg.h"
30
31/*
32 * We want to wake up in delay microseconds. If timeval is larger
33 * than delay, set timeval to delay.
34 */
35bool
36shaper_soonest_event(struct timeval *tv, int delay)
37{
38 bool ret = false;
39 if (delay < 1000000)
40 {
41 if (tv->tv_sec)
42 {
43 tv->tv_sec = 0;
44 tv->tv_usec = delay;
45 ret = true;
46 }
47 else if (delay < tv->tv_usec)
48 {
49 tv->tv_usec = delay;
50 ret = true;
51 }
52 }
53 else
54 {
55 const int sec = delay / 1000000;
56 const int usec = delay % 1000000;
57
58 if (sec < tv->tv_sec)
59 {
60 tv->tv_sec = sec;
61 tv->tv_usec = usec;
62 ret = true;
63 }
64 else if (sec == tv->tv_sec)
65 {
66 if (usec < tv->tv_usec)
67 {
68 tv->tv_usec = usec;
69 ret = true;
70 }
71 }
72 }
73#ifdef SHAPER_DEBUG
74 dmsg(D_SHAPER_DEBUG, "SHAPER shaper_soonest_event sec=%" PRIi64 " usec=%ld ret=%d",
75 (int64_t)tv->tv_sec, (long)tv->tv_usec, (int)ret);
76#endif
77 return ret;
78}
79
80void
82{
83 CLEAR(s->wakeup);
84}
85
86void
88{
89 msg(M_INFO, "Output Traffic Shaping initialized at %d bytes per second", s->bytes_per_second);
90}
#define D_SHAPER_DEBUG
Definition errlevel.h:175
#define M_INFO
Definition errlevel.h:54
#define CLEAR(x)
Definition basic.h:32
#define dmsg(flags,...)
Definition error.h:170
#define msg(flags,...)
Definition error.h:150
void shaper_msg(struct shaper *s)
Definition shaper.c:87
void shaper_reset_wakeup(struct shaper *s)
Definition shaper.c:81
bool shaper_soonest_event(struct timeval *tv, int delay)
Definition shaper.c:36
struct timeval wakeup
Definition shaper.h:49
int bytes_per_second
Definition shaper.h:48