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